When using inheritance, you can specify the virtual
keyword:
struct A{};
struct B: public virtual A{};
When class B
has virtual base A
it means that A
will reside in most derived class of inheritance tree, and thus that most derived class is also responsible for initializing that virtual base:
struct A
{
int member;
A(int param)
{
member = param;
}
};
struct B: virtual A
{
B(): A(5){}
};
struct C: B
{
C(): /*A(88)*/ {}
};
void f()
{
C object; //error since C is not initializing it's indirect virtual base `A`
}
If we un-comment /*A(88)*/
we won't get any error since C
is now initializing it's indirect virtual base A
.
Also note that when we're creating variable object
, most derived class is C
, so C