I'm reading classes tutorial in cplusplus.com.
I got confused by the following paragraph.
Default-constructing all members of a class may or may always not be convenient: in some cases, this is a waste (when the member is then reinitialized otherwise in the constructor), but in some other cases, default-construction is not even possible (when the class does not have a default constructor). In these cases, members shall be initialized in the member initialization list.
So, my question is what does the "when the member is then reinitialized otherwise in the constructor" mean? Why is a waste?
In the beginning, I think the "reinitialized` like following code.
class Son
{
int age;
public:
// default constructor
Son()
{
age = 1;
}
Son(int age) : age(age) {}
};
class Father
{
Son son; // First, I think that it will call default constructor of class Son when the object of Father was created
int age;
public:
// Then object of Father will call this constructor, then initialize son again.
Father(int sonAge, int fatherAge) : son(sonAge), age(fatherAge)
{
}
};
Then, I found Son son
wasn't to define son at all, it just waited the constructor of Father to initialized son
. So this isn't waste, my idea is wrong!!! Maybe I lack the knowledge of object creation order? cplusplus.com provides tutorial seems incomplete for me...
Can you give me a few code examples?