Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor call hierarchy

I'm having a tricky time with calling rules for constructors in a type hierarchy. Here is what I do:

class A{
protected:
    int _i;
public:
    A(){i = 0;}
    A(int i) : _i(i){}
    virtual ~A(){}
    virtual void print(){std::cout<<i<<std::endl;}
};

class B : virtual public A{
protected:
    int _j;
public:
    B() : A(){_j = 0;}
    B(int i, int j) : A(i), _j(j){}
    virtual ~B(){}
    virtual void print(){std::cout<<i<<", "<<j<<std::endl;}
};

class C : virtual public B{
protected:
    int _k;
public:
    C() : B(){_k = 0;}
    C(int i, int j, int k} : B(i,j), _k(k){}
    virtual ~C(){}
    virtual void print(){std::cout<<i<<", "<<j<<", "<<k<<std::endl;}
};

int main(){
    C* myC = new C(1,2,3);
    myC->print();
    delete myC;
    return 0;
}

Now, I would like to have new C(1,2,3) call the constructor of B(1,2) which then in turn should call the constructor A(1) to store _i=1, _j=2, _k=3. When creating the instance myC of the class C, for some reason I don't understand, however, the first constructor to be called is the standard constructor of A, i.e., A::A(); This obviously leads to wrong results, as the protected variable _i is assigned the value 0. The constructor A(1) is never called. Why is this so? I find this very counter intuitive. Is there some way to avoid explicitly calling all constructors within the type-hierarchy to achieve the desired behavior?

Thx for the help!

like image 511
user1999920 Avatar asked Jan 22 '13 10:01

user1999920


1 Answers

do you really need a virtual inheritance here? you've got a problem because a very first virtual base ctor will be called first, but you don't specify any when inherit C from B (latter already have A virtually inherited, so default was called).

one solution is to remove virtual inheritance... as mentioned in the answer of Arne Mertz. another (if you really want virtual inheritance) is to call A explicitly from C ctor:

C(int i, int j, int k} : A(i), B(i,j), _k(k){}
like image 111
zaufi Avatar answered Nov 15 '22 16:11

zaufi