virtual inheritance diamond malformed

Both C1 and C2 should virtually derive from Base. You see the D class instantiation only calling Base ctor once. See http://www.parashift.com/c++-faq-lite/virtual-inheritance-where.html

Now, what if we omit one of the “virtual” keywords? Base ctor called twice — tested in GCC

struct B {
    B() {
        cout << "B\n";
    }
};
struct C1: virtual public B {
    C1() {
        cout << "entering C1()\n";
    }
};
struct C2: virtual public B {
    C2() {
        cout << "entering C2()\n";
    }
};
class D: public C1, C2 {
};

int main() {
    D d;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s