Re: Virtual inheritance
On Apr 30, 3:53 am, "Massimo" <bar...@mclink.it> wrote:
Hi to all, I'm facing a problem in a particularly complex inheritance
hierarchy, and I'd like to know what the standard says about it and if my
compiler is correct in what it does.
I have two consecutive layers of diamond-shaped inheritance; the first la=
yer
is declared as using virtual inheritance, while the second one is declared
as *not* using it.
This is what I'd like to have:
Base Base
/ \ / \
D1Base D2Base D1Base D2Base
\ / \ /
Middle Middle
|| ||
D1Middle D2Middle
\ /
\ /
\ /
Last
Which is impossible in C++.
This is what the compiler actually does:
Base
/ > D1Base D2Base
\ /
Middle
/ > D1Middle D2Middle
\ /
Last
That's not what it does given the code you posted. What you're
actually getting is more like.
------Base------
/ / \ \
/ / \ \
D1Base D2Base D1Base D2Base
\ / \ /
Middle Middle
|| ||
D1Middle D2Middle
\ /
\ /
\ /
Last
If I remove all virtual inheritance, everything goes just
fine; if I use it in the first layer, there seems to be no way
of disabling it afterwards.
Virtual inheritance only affects the classes which derive using
it. On the other hand, all instances of all classes which
derive from Base will share a common instance. You can't group,
with two or three different groups of classes sharing a common
instance.
What you might try is making Base, the two DnBase and Middle
templates (without changing anything else), and instantiating
them on eithre D1Middle or D2Middle. By doing this, the 2
Middle, each of the DnBase, and the two Base are formally
different types. This could result in significant code
duplication, of course.
Does the standard saying anything about this, or is it
implementation-dependant? My compiler is Visual Studio 2005.
The standard specifies very precisely what abstract graph you
should end up with. All of the compilers I have access to
(VC++, Sun CC and g++) are conform in this respect, and
generate to diagram I've given with your code.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34