Re: Virtual inheritance
On Apr 29, 9:25 pm, "Massimo" <bar...@mclink.it> wrote:
"jg" <jgu...@gmail.com> ha scritto nel messaggionews:1177906176.761735.176510@n76g2000hsh.googlegroups.com...
I think the object of Last will have a single subobject of Base,
not two. The other subobjects are not shared (i.e. there are
two subobjects each for D1Base, D2Base, Middle).
Base
/ / \ \
/ / \ \
/ / \ \
D1Base D2Base D1Base D2Base
\ / \ /
Middle Middle
| |/
D1Middle D2Middle
\ /
\ /
\ /
Last
I really don't know, but don't think so, since each and every one of these
classes is (somewhat) a subclass of Base, and so inherits (or seems to
inherit) the virtual inheritance setting.
I'm actually quite sure there's only one Middle object, because the compiler
doesn't complain about method names resolution issues... while it *does*, if
inheritance is made non-virtual.
Anyway, any clue on how to obtain the desidered behaviour (if this is at all
possible)?
The problem is that every class that declares a virtual base class
shares that base class object with every other class that has declared
the same virtual base class. Therefore it is not possible to declare
D1Base and D2Base in such a way that the two pairs of D1Base and
D2Base objects share the same Base object as one another - without
also having them share the same Base object as every other pair of
D1Base and D2Base objects in the hierarchy. In other words, it is
possible to reduce the top of the current inheritance graph to a
single Base object - or three or four Base class objects - but not two
- as the graph is currently structured.
One way to remedy this problem - and to have the two Base objects at
the top of the hierarchy as desired - would be to insert a pair of
"buffer" classes, Base1 and Base2, between the Base and the D1Base and
D2Base objects. D1Base and D2Base would then each inherit from both
Base1 and Base2 as virtual base classes. The rest of the current
inheritance hierarchy would remain the same.
To illustrate. With this change the top level of the inheritance graph
would like this:
Base Base
| |
| |
Base1 Base2
| | | |
| \ / |
| \ / |
| \/ |
| /\ |
| / \ |
| / \ |
| | | |
D1Base D2Base
And the revised class declarations at the top of hierarchy would look
like:
class Base
{
int n;
};
class Base1 : public Base
{
};
class Base2 : public Base
{
};
class D1Base : public virtual Base1, public virtual Base2
{
};
class D2Base : public virtual Base1, public virtual Base2
{
};
The rest of the inheritance hierarchy would remain the same.
Greg