>will anything that is passed to whatever be assigned to x_ using the initializing list?
Assuming whatever is a compatible initializer for x_, yes.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Another small thing to add that I read in the book C++ in Action .
Say you have a const member declared inside the C++ class.
class Planet
{
public:
Planet( int iWeight);
private:
const int weight;
}
since the weight member cant be assigned a value in a standard contructor like this
Planet::Planet( int iWeight)
{
weight = iWeight ; // wrong
}
The only way you can do that is using an initialization list.
Planet::Planet( int iWeight):weight(iWeight) // correct
{
}
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
The accessibility of the data member does not matter here. Even if you declare weight public you will not be able to use the standard constructor.
What we use the initialization list here is for initializing const data members in a class.
We have declared weight to be const. So when you define an object of type Planet, you must give a weight for it.
e.g.
Planet earth( 2000 );
In the standard constructor you use the
weight = iWeight
statement which is illegal.
The only way is to use the initialization list.
Look for thisThe keyword const means that nobody can change the value of _identifier during the lifetime of the object. It is physically (or at least "compilatorily") impossible. But even a const has to be initialized some place or another. The compiler provides just one little window when we can (and in fact have to) initialize a const data member. It is in the preamble to the constructor. Not even in the body of the constructor--only in the preamble itself can we initialize a const data member. Preamble is the part of the constructor that starts with a colon and contains a comma-separated list of data members followed by their initializers in parentheses. In our preamble _identifier is initialized with the value of id. in this page of the book I have linked. you will get a more clear idea of it's usage.
WolfPack
Postaholic
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115