Virtual inheritace -- when one inheritance of the base is virtual andthe other isn't.

P

pauldepstein

The following is copy-pasted from the FAQ

BEGIN QUOTE

[25.9] Where in a hierarchy should I use virtual inheritance?

Just below the top of the diamond, not at the join-class.

To avoid the duplicated base class subobject that occurs with the
"dreaded diamond", you should use the virtual keyword in the
inheritance part of the classes that derive directly from the top of
the diamond:

class Base {
public:
...
protected:
int data_;
};

class Der1 : public virtual Base {
public: ^^^^^^^$B!=(Bthis is the key
...
};

class Der2 : public virtual Base {
public: ^^^^^^^$B!=(Bthis is the key
...
};

class Join : public Der1, public Der2 {
public:
void method()
{
data_ = 1; $B"+(B good: this is now unambiguous
}
};

int main()
{
Join* j = new Join();
Base* b = j; $B"+(B good: this is now unambiguous
}

Because of the virtual keyword in the base-class portion of Der1 and
Der2, an instance of Join will have have only a single Base subobject.
This eliminates the ambiguities. This is usually better than using
full qualification as described in the previous FAQ.

For emphasis, the virtual keyword goes in the hierarchy above Der1 and
Der2. It doesn't help to put the virtual keyword in the Join class
itself. In other words, you have to know that a join class will exist
when you are creating class Der1 and Der2.

Base
/ \
/ \
virtual / \ virtual
Der1 Der2
\ /
\ /
\ /
Join

END QUOTE

What would happen if a programmer (by mistake, for example) made the
inheritance of Der2 non-virtual but kept the inheritance of Der1
virtual:

class Der2 : public Base {
public:
...
};

Is there any difference between the situation with neither of the two
inheritances virtual and the situation with exactly one inheritance
virtual?

Thanks,

Paul Epstein
 
A

Alf P. Steinbach

* (e-mail address removed):
[snip]
Base
/ \
/ \
virtual / \ virtual
Der1 Der2
\ /
\ /
\ /
Join

END QUOTE

What would happen if a programmer (by mistake, for example) made the
inheritance of Der2 non-virtual but kept the inheritance of Der1
virtual:

class Der2 : public Base {
public:
...
};

Then Der2 has one Base sub-object, and Der1 has another. The virtually
inheriting classes share one single Base sub-object. The others don't.

Cheers & hth.,

- Alf


PS: Please post with Latin-1 character set, or UTF-8. Cause' when it's Japanese
or Chinese my Thunderbird chooses a font that's very hard on the eyes. And I'm
too lazy to fix that, which I presume would involve delving around in a
spaghetti mess of options.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top