c++virtual-inheritance

Virtual but not multiple inheritance to call grandparent constructor


I'm having this kind of code:

class Ref {<undefined>};
Ref refObjectForA, refObjectForB;

class Base
{
  public:
    Base(const Ref & iRef) : _ref(iRef) {}
    virtual ~Base() {}

    const Ref & ref;
};

class A: public Base
{
  public:
     A() : Base(refObjectForA) {}
     virtual ~A() {}
};

class B: public A
{
  public:
    B() : Base(refObjectForB) {} // won't compile: Base is not direct base of B
    virtual ~B() {}
};

As the attribute is a reference, I think I can only set it in constructor, so I need to call Base constructor in B(). I've found two ways: providing a "forward" constructor in A (but this implies adding code in all classes that might be inherited):

A(const Ref& iRef): Base(iRef)

or using virtual inheritance:

class A: public virtual Base

Second option allows more straightforward code in B implementation but I'm wondering if I'm misusing virtual inheritance in an ugly trick or if it is a valid usecase.

One of the "unexpected" behaviors I've found is that it's not possible to static_cast a Base pointer to a B pointer because of the virtual inheritance.

Moreover I'm also wondering why it works (I mean why a B().ref == refObjectForB): I would think that the implicit call to default A() constructor in B() would overwrite the ref attribute after explicit Base constructor, but maybe it's not true with virtual inheritance.


Solution

  • The best option I can see if you want to stick to your inheritance hierarchy is to implement protected constructors taking a reference which they'll forward to the Base class. Making a constructor protected makes sure that a (final) instance can't be constructed using this constructor, so it will only be used in subclasses to initialize the super classes.

    With some more or less ugly and dangerous macro, this becomes easy to be written:

    #define REF_FORWARD_CTOR(ClassName, DirectSuperClassName) \
        protected: ClassName(class Ref &r) : DirectSuperClassName(r) {} \
        public:
    
    class A : public Base
    {
        REF_FORWARD_CTOR(A, Base)
    public:
        A() : Base(refObjectForA) {} // normal ctor
    };
    
    class B : public A
    {
        REF_FORWARD_CTOR(B, A)
    public:
        B() : A(refObjectForB) {} // normal ctor
    };
    

    An alternative design would be to let A and B both derive (directly) from Base. Then, add functionalities by using multiple inheritance and "common classes", maybe private, depending on what they are for:

    class Base {
    };
    
    class Common {
        // common stuff used by both A and B
    };
    
    class A : public Base, public Common {
        // no further stuff here
    };
    
    class B : public Base, public Common {
        // add more stuff, or put it in a common super-class again,
        // if some classes want to inherit from B again
    };
    

    The problem with this design is that functionality in Common can't access the stuff in A and B. To solve this, do one of the following: