HI, I have a question but cannot find the answer for a long time, that is, what's difference between the following 2 statements about the parameter initialization?
class A::A()
: a(0), b(0), c(0)
{
}
class A::A()
{
a = 0
b = 0;
c = 0;
}
I know there is "direct initialization" and "copy initialization", but I don't know what's the other differences and if there is any description about the first statement?
Thanks in advance
Using the initializer list, the members are created and initialized only once, with the given value.
Using assignment, the members are initialized with a default value, then reassigned in the constructor body.
In some cases (constants, references) you can only use initialization lists because
In other cases, even though assignment is possible, initialization list is still preferable as it avoids extra work (double initialization, which can be costly for some types) and adheres to a common idiom, making your code easier to understand and maintain.
One caveat which is good to know is that the order of initialization of members is defined by their order of declaration, not by the order in which they are listed in the initialization list. E.g.
class Example {
int a, b, c;
Example() : a(1), c(2), b(c) {}
}
yields undefined behaviour because b
is initialized before c
, hence with an undefined value. To avoid confusion and the potential for such subtle bugs, always list members in the initialization list in the same order as they're declared in the class.
This may seem obscure at first, but there is a reason for it. In C++, it is guaranteed that all members of a class are destroyed in exactly the reverse of the order they were created. Now, classes can have multiple constructors, each with its own initialization list, and (unfortunately, one might say) initialization lists can be ordered any way the programmer wants. If the order of the initialization list determined the actual order of initialization, the runtime should somehow maintain data about each object to remember which constructor it was created with, and in what order its members should be destroyed. This would incure runtime overhead, for no obvious benefit, so - in line with the general C++ philosophy of "pay only for what you use" - it was decided that the initialization order is defined by the declaration order once and for all.
The purpose of default initialization list is to initialize the constant variable with in the class. Because the constant variable is initialized before the object is initialized.
I provide one sample to explain the difference between these two initializations:
class A
{
private:
const int x;
};
A::A():x(5) //this code works fine
{
}
A::A() //this code is wrong.const variable is not initialized once object
{
x=5;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With