On some tutorials (e.g. http://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm) I read that the following two codes are equivalent.
First code:
class MyClass1{
public:
int a;
int b;
MyClass1(int a, int b) : a(a), b(b) {};
};
Second code:
class MyClass2{
public:
int a;
int b;
MyClass2(int, int);
};
MyClass2::MyClass2(int a, int b){
this->a = a;
this->b = b;
}
In fact, they give me the same results. But, if I use const
members I'm not able to compile the code anymore.
class MyClass1{
public:
const int a;
const int b;
MyClass1(int a, int b) : a(a), b(b) {};
};
class MyClass2{
public:
const int a;
const int b;
MyClass2(int, int);
};
MyClass2::MyClass2(int a, int b){
this->a = a;
this->b = b;
}
In fact the first class give me no error but in the second class there is an assignment of read-only member
. So, these are the questions:
What is the real difference among the two methods of initialization?
Is using the initialization lists the only method to initialize const
members of a class?
Note: I read online the use of delegating constructors to avoid this problem but it's not clear for me their use and what they really do.