such is the code,and with the error:”illegal member initialization: ‘a’ is not a base or member”,what is the meaning of the error info,and why??
class A{
public:
int a;
};
class B:public A{
public:
B();
};
B::B():a(10){ // put "a(10)" into the constructor body is right
}
By the time the constructor of the derived class is invoked, the base class must already be constructed. So it’s already too late. To see why it must be this way, consider:
Now, think about it. The
Middleconstructor has to run before theDerivedconstructor. And theMiddleconstructor ensures thatiis 2. So how can theDerivedconstructor later re-construct it with a different value?