breaking virtual inheritance avoiding explicit base ctor

Viewed 69

Following explanation and examples from here I have exemplary constructed the following inheritance model creating a diamond

class Base {
public:
    int data_;
    Base(int d) : data_(d) {}
    Base() = deleted;
 };

class A : public virtual Base {
public:
    A() : Base(42) {};
};

class B : public virtual Base {
public:
    B() : Base(24) {};
}

class AB : public A, public B {
public:
    AB() : Base(-1) {};
}

so far so good; note, that AB() needs to call to the Base(int)-ctor now. This is kind of understandable, because having the alternative initializer branches of going through AB>>A>>Base or AB>>B>>Base would not result in a well defined behavior at that.

Lets branch out from class A into a side-branch before we even have closed the diamond:

class A_Child : public A {
public:
    A_Child() : A() {}; // not permitted by compiler
}

This will not compile, as the compiler will explicitly ask for to specify the ctor Base(int) in the inializer list of A_Child.

I don't quite understand this behavior; as we are no longer virtually inheriting at this point and the path of initalization of A_Child>>A>>Base is not ambiguous.

It seems now for every furthermore derived class of A_Child I have to again specify the Base(int) initializer explicitly. This is kind of breaking the encapsulation as every code that derives from this class needs to know how the class at the very base acts and is implemented.

Is there any way to stop or to break the virtual inheritance once I branch into a side-line?

0 Answers
Related