954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

please explain initializer list.

hi everyone, i m messed up with intializer lis.
please help me by solving these questions.
How does compiler impliments intializer lits?
How intializer lits improve performence?
When it is must to use intializer lits?
When intializer lits must not be used?
What is wrong with this pice of code..

class A {
   private:
       int x, y;
  
   public:
      A():y(0), x(y) 
      {
      }
}
dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

>How does compiler impliments intializer lits?
It depends on the compiler.

>How intializer lits improve performence?
There are two steps when you initialize in the body of a constructor. The first step is performed before the body, where the data members are allocated memory and given a default value. The second step is the actual initialization that you ask for. So there's an unnecessary step where the data members are given a default value. An initialization list avoids that step and can make your code faster as a result, especially if copying data is expensive for the type that's being initialized.

>When it is must to use intializer lits?
const and reference data members *must* be initialized in an initialization list because they can't exist without being initialized. Everything else is optional.

>When intializer lits must not be used?
There isn't one.

>What is wrong with this pice of code..
You didn't terminate your class with a semicolon.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

hi Narue thanx for your help..
>>What is wrong with this pice of code..
have a look of declaration of these two classes, declareing first one leads to junk initialization of int x, but the other one initialize int y to 0.
what i figured out is, since x comes earlier in class decleratin it should be initialized first, in other words initializer list should follow a sequence. what is ur view?

class A {
   private:
       int x, y;
   public:
      A():y(0), x(y) 
      {
      }
};
--------------------------------------
class A {
   private:
       int x, y;
   public:
      A():x(0), y(x) 
      {
      }
};
dubeyprateek
Junior Poster
176 posts since Mar 2006
Reputation Points: 39
Solved Threads: 24
 

>what is ur view?
My view is the same as C++'s view. Data members are initialized in the order that they're declared, regardless of the order of the initialization list. Therefore, x is always initialized first, followed by y. It's generally safer to avoid having data members depend on each other during initialization.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>How does compiler impliments intializer lits? It depends on the compiler.

>How intializer lits improve performence? There are two steps when you initialize in the body of a constructor. The first step is performed before the body, where the data members are allocated memory and given a default value. The second step is the actual initialization that you ask for. So there's an unnecessary step where the data members are given a default value. An initialization list avoids that step and can make your code faster as a result, especially if copying data is expensive for the type that's being initialized.

>When it is must to use intializer lits? const and reference data members *must* be initialized in an initialization list because they can't exist without being initialized. Everything else is optional.

>When intializer lits must not be used? There isn't one.

>What is wrong with this pice of code.. You didn't terminate your class with a semicolon.


Explained in Detail,
Thank you

NaveenGowdar
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You