class base{
public:
base(constchar* label) : m_label(label){};
virtual ~base(){};
void init(){};
constchar *get_label() { return m_label; };
protected:
constchar *m_label;
};
class O1 : publicvirtual base
{
public:
O1(constchar *label) : base(label){};
virtual ~O1(){};
};
class O2 : publicvirtual base
{
public:
O2(constchar *label) : base(label){};
virtual ~O2(){};
};
class O12 : public O1, public O2
{
public:
O12(constchar *label) : ???(label){};
virtual ~O12(){};
};
The ??? is the problem. Both with and without explicitly calling an inherited constructor fail. The compiler gripes about not finding a matching constructor for base. Unless I change "???" to "base". Then it gripes about not finding a constructor for one of the other classes (O1 or O2). Is it (perchance) because of the base constructor's arg?
Any pointers to getting something like this working?
Virtual base objects need to be initialized before the other objects, but since you don't have a default constructor, you still need to call the other parent objects explicitly after the base object.
1 2 3 4 5 6 7
class O12 : public O1, public O2
{
public:
O12(constchar *label) : base(label), O1(label), O2(label) {};
virtual ~O12(){};
};
Because there is only a single instance of a virtual base class that is shared by multiple classes that inherit from it, the constructor for a virtual base class is not called by the class that inherits from it (which is how constructors are called, when each class has its own copy of its parent class) since that would mean the constructor would run multiple times. Instead, the constructor is called by the constructor of the concrete class. In the example above, the class radio directly calls the constructor for storable. If you need to pass any arguments to the storable constructor, you would do so using an initialization list, as usual:
radio::radio ()
: storable( 10 ) // some value that storable needs
, transmitter()
, receiver()
{}
It was the concrete part that I didn't get at first. Specifically, the base constructor is called first, by the merged class o12. The gotcha to watch out for is that neither o1 nor 02 mess around with base in a way that would adversely affect either of them.