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) 
      {
      }
}

Recommended Answers

All 4 Replies

>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.

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) 
      {
      }
};

>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.

>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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.