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
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