73

Why must class members declared as const be initialized in the constructor initializer list rather than in the constructor body?

What is the difference between the two?

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
Priyanka Mishra
  • 10,400
  • 18
  • 62
  • 75

3 Answers3

105

In C++, an object is considered fully initialised when execution enters the body of the constructor.

You said:

"i wanted to know why const must be intialized in constructor initializer list rather than in it's body ?."

What you are missing is that initialisation happens in the initialisation list, and assignment happens in the body of the constructor. The steps in logic:

1) A const object can only be initialised.

2) An object has all of its members initialised in the initialisation list. Even if you do not explicitly initialise them there, the compiler will happily do so for you :-)

3) Therefore, putting 1) and 2) together, a member which is const can only ever have a value assigned to it at initialisation, which happens during the initialisation list.

Vladimir
  • 1,096
  • 1
  • 7
  • 4
  • 3
    Congrats for your first answer... ^_^ – paercebal Dec 10 '08 at 09:29
  • 2
    About no.2. ASFAIK this is true only for non-builtin types with default ctors. I would say that no.1 together with what you stated above the list is sufficient to answer the question. Anyway, good answer +1 from me! – Andreas Magnusson Dec 10 '08 at 09:43
  • 1
    @AndreasMagnusson: Before C++11, default initialization is performed for all members not named in the initializer list, including builtin primitive types (for which default initialization means "no initialization at all"). If there is no (accessible) default constructor, then it is a compile error. Then brace-or-equal-initializers changed all this. – Ben Voigt Nov 14 '15 at 04:02
  • @BenVoigt: That may be, but it's quite superfluous information. For most programmers the difference between a variable being initialized as "no initialization at all" or not being initialized is purely a question of English semantics. At the end of the line, unless the programmer explicitly assigns a value to such a variable, its value will remain undefined (which in turn is a cause for a great many bugs). Simply saying that it gets initialized means it's much too easy to interpret as it gets initialized to `0` (which *is* the case in a great many other programming languages). – Andreas Magnusson Nov 19 '15 at 08:39
10

const and reference variables must be initialized on the line they are declared.

 class Something  
 {  
     private:  
      const int m_nValue;  
     public:  
      Something()  
      {  
          m_nValue = 5;  
      }  
  };

would produce code equivalent to;

const int nValue; // error, const vars must be assigned values immediately  
nValue = 5; 

Assigning const or reference member variables values in the body of the constructor is not sufficient.

C++ provides another way of initializing member variables that allows to initialize member variables when they are created rather than afterwards. This is done through use of an initialization list.

You can assign values to variables in two ways: explicitly and implicitly: view plaincopy to clipboardprint?

int nValue = 5; // explicit assignment  
double dValue(4.7); // implicit assignment  

Using an initialization list is very similar to doing implicit assignments.

Remember that the member initialization list, used to initialize base and member data objects, is in the definition, not declaration of the constructor.

More on cpp-tutorial and Code Wrangler.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
-2

Because constant variables and references must be initialized at time of declaration i.e before use. But Constructors will assign value to a varaible not initailize the variable therefore you must use initailizier list for constant and references

user3651946
  • 321
  • 2
  • 6