virtual base classes and constructor calls

Discussion in 'C++' started by Lefevre, Jul 30, 2003.

  1. Lefevre

    Lefevre Guest

    Hello

    I recently had troubles with a class inheritance hierarchy.

    I solved it, but it didn't satisfied me.

    I found the solution using this forum :)

    Actualy i found the following message (with no responces associated) :


    ----------------------------------
    >
    > I ran into an interesting situation with respect to multiple
    > inheritance and virtual base classes. The following example
    > illustrates the problem:
    >
    > class A {
    > A(int) { /*... */ ; }
    > };
    >
    > class B : virtual public A {
    > B(int a, int b) : A(a) { /* ... */ ; }
    > };
    >
    > class C : virtual public A {
    > C(int a, int c) : A(a) { /* ... */ ; }
    > };
    >
    > class D : public B, public C {
    > D(int a, int b, int c, int d) : A(a), B(a,b), C(a,c) { /* ... */; }
    > }


    > class E : public D {
    > #ifdef WIBNI /* [1] */
    > // what I would LIKE to be able to do
    > E(int a, int b, int c, int d, int e) : D(a,b,c,d) { /* ... */; }
    > #else
    > // what I actually HAVE to do
    > E(int a, int b, int c, int d, int e) : A(a),D(a,b,c,d) { /* ... */; }
    > #endif
    > }
    >
    > /* [1] Wouldn't It Be Nice If */
    >
    > The English explanation: My inheritance graph is as follows:
    >
    > A
    > / \
    > B C
    > \ /
    > D
    > |
    > E
    >
    > "A" is a virtual base class of "B" and "C". "A" does not have a
    > default constructor (my application is a bit more involved than the
    > above).
    >
    > "B" and "C" are base classes of "D". I understand and support that
    > "D"'s constructor(s) must specify how the virtual base class "A" is
    > constructed. (If this was not the case, there could be ambiguities in
    > how "A" was actually constructed, given that the calls to "A"'s
    > constructor from "B" and from "C" could be different.) This is
    > explained in the ARM S12.6.2, pp. 292--4.
    >
    > What I don't understand is why "E" must also declare how the virtual
    > base class "A" is to be constructed - shouldn't the compiler be able
    > to figure out that the "virtualness" of "A" at "E" comes about at "D",
    > and thus do the right thing:
    > 1) try to construct a "D", the base class of "E"
    > 2) note that "A" is a multiply-inherited virtual base class of
    > "D" - and thus that "A"'s constructor must be called with
    > the arguments as specified by "D"'s constructor ("A"'s
    > default constructor if none is declared).
    > 3) construct "A"
    > 4) try to construct "B" - the first base class of "D"
    > 5) note that "A" is a virtual base class of "B" - note that
    > "A" has already been constructed - "B"'s "A" is done -
    > ignore)
    > 6) execute "B"'s constructor
    > 7) try to construct "C" - the second base class of "D"
    > 5) note that "A" is a virtual base class of "C" - note that
    > "A" has already been constructed - "C"'s "A" is done -
    > ignore)
    > 6) execute "C"'s constructor
    > 7) execute "D"'s constructor
    > 8) execute "E"'s constructor
    >

    .......
    ------------------------------------------------


    I truncated it because the rest of it wasn't relevent to my problem.

    If you wish, you can find the original message with google forum
    search engine using the following key words :
    "virtual class bad constructor call"

    My question is simple : what could have been answered to this post ?
    I think there must be a reason why this haven't been done this way in
    the C++ standard, but i can't figure why.

    Would such a compiler be too slow ?

    I wander.

    Regards

    Benoit Lefevre.
     
    Lefevre, Jul 30, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Winter
    Replies:
    9
    Views:
    617
    Michael Winter
    Sep 24, 2003
  2. Honne Gowda A
    Replies:
    2
    Views:
    878
    Karl Heinz Buchegger
    Oct 31, 2003
  3. mrstephengross
    Replies:
    5
    Views:
    597
    Larry I Smith
    May 18, 2005
  4. Replies:
    2
    Views:
    715
  5. Marcel Müller
    Replies:
    2
    Views:
    121
    Marcel Müller
    Mar 15, 2014
Loading...

Share This Page