Topic: Initializing multiple virtual base classes


Author: jgottman@carolina.rr.com ("Joe Gottman")
Date: Sun, 24 Aug 2003 15:49:26 +0000 (UTC)
Raw View
Suppose I have class hierarchy that looks like the following:

class A1 {};
class A2 {};

class B : public virtual A1, public virtual A2 {};
class C: public virtual A2, public virtual A1 {}; //Note order!!

class D : public B, public C {};

When D's constructor is called, will its A1 subobject or its A2 subobject be
constructed first, or is this code ill-formed?

Joe Gottman

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: ralf.schneeweiss@onlinehome.de ("Ralf Schneewei ")
Date: Mon, 25 Aug 2003 15:49:49 +0000 (UTC)
Raw View
Hi,
I think, there is no rule inside the standard. With each compiler and
possibly in each situation, you get another result.

Ralf Schneewei
http://www.oop-trainer.de



""Joe Gottman"" <jgottman@carolina.rr.com> schrieb im Newsbeitrag
news:0c32b.19527$J16.763999@twister.southeast.rr.com...
> Suppose I have class hierarchy that looks like the following:
>
> class A1 {};
> class A2 {};
>
> class B : public virtual A1, public virtual A2 {};
> class C: public virtual A2, public virtual A1 {}; //Note order!!
>
> class D : public B, public C {};
>
> When D's constructor is called, will its A1 subobject or its A2 subobject
be
> constructed first, or is this code ill-formed?
>
> Joe Gottman
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net (James Kuyper)
Date: Mon, 25 Aug 2003 16:48:26 +0000 (UTC)
Raw View
jgottman@carolina.rr.com ("Joe Gottman") wrote in message news:<0c32b.19527$J16.763999@twister.southeast.rr.com>...
> Suppose I have class hierarchy that looks like the following:
>
> class A1 {};
> class A2 {};
>
> class B : public virtual A1, public virtual A2 {};
> class C: public virtual A2, public virtual A1 {}; //Note order!!
>
> class D : public B, public C {};
>
> When D's constructor is called, will its A1 subobject or its A2 subobject be
> constructed first, or is this code ill-formed?

12.6.2p5: "... virtual base classes shall be initialized in the order
they appear on a depth-first left-to-right traversal of the directed
acyclic graph of base classes, where "left-to-right" is the order of
appearance of the base class names in the derived class
_base-specifier-list_."

The specified traversal reaches the B base class before the C base
class, and therefore initializes first A1, then A2. By the time it
reaches the C base class, they've already been initialized, and
further initialization is ignored.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]