Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why constant data member of a class need to be initialized at the constructor?

I want to know why constant data member of a class need to be initialized at the constructor and why not somewhere else? What is the impact of doing so and not doing so?

I also see that only static constant integral data can be initialized inside the class other than that non of the data members can be initialized inside the class.

for eg:- Suppose below is my class declaration

class A{
     int a; // This we can initialize at the constructor or we can set this member by calling "vSet" member function
     const int b;
     static const int c = 10; //This works fine
public:
     A();
     ~A();
     void vSet(int a);
     int iAdd();
     void vDisplay();    
};

And the constructor is defined as mentioned below:-

Edited Section: As previous constructor definition example was wrong

 A::A():a(1),b(9){}

Please correct me if I am wrong. Thanks in advance.

like image 244
Abhineet Avatar asked May 18 '12 05:05

Abhineet


People also ask

Why a constant must be initialized when it is declared?

When a variable is declared as const it means that , variable is read-only ,and cant be changed . so in order to make a variable read only it should be initialized at the time it is declared.

Why do we initialize in constructor?

A constructor is typically used to initialize instance variables representing the main properties of the created object. If we don't supply a constructor explicitly, the compiler will create a default constructor which has no arguments and just allocates memory for the object.

How are constant data members of a class initialized?

To initialize the const value using constructor, we have to use the initialize list. This initializer list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon. members will be separated using comma.

Can we initialize constant variable in constructor?

Yes, you can also initialize these values using the constructor.


1 Answers

A::A(){
      a = 1;
      b = 9; // Why we need to initialize this only at the constructor. 
   }

Is not initialization but it is Assignment.
a and b are already constructed and you assign them values in this case. The const qualifier demands that the variable not be changed after its initialization, allowing this assignment would break that contract.

This is Initialization using Member Initialization list.

A::A():a(1),b(9)
{}

You might want to have a look at this answer of mine to know the difference:

What is the difference between Initializing and Assignment inside constructor?


As for another question, regarding only static constant integral data can be initialized inside the class, please read this answer of mine which explains in greater detail:

Why I can't initialize non-const static member or static array in class?

like image 113
Alok Save Avatar answered Sep 28 '22 05:09

Alok Save