Initializer lists and multiple constructors

Discussion in 'C++' started by Richard, Mar 1, 2007.

  1. Richard

    Richard Guest

    If class C inherits from class B, which inherits from class A, is the
    order of the initializer list in the following constructor
    insignificant (C only inherits from A because it inherits from B)?:

    C::C() :
    A(),
    B()
    {
    }

    That is, would:

    C::C() :
    B(),
    A()
    {
    }

    work just the same? What if none of these were default constructors
    and some arguments to C's constructor where passed to the parent class
    constructors?

    Now, what if B's constructor was:

    B::B() :
    A()
    {
    }

    Upon invoking the constructor for C, would A's constructor be called
    twice?
     
    Richard, Mar 1, 2007
    #1
    1. Advertising

  2. Richard

    Ian Collins Guest

    Richard wrote:
    > If class C inherits from class B, which inherits from class A, is the
    > order of the initializer list in the following constructor
    > insignificant (C only inherits from A because it inherits from B)?:
    >
    > C::C() :
    > A(),
    > B()
    > {
    > }
    >
    > That is, would:
    >
    > C::C() :
    > B(),
    > A()
    > {
    > }
    >

    Neither of these should compile. You can only call the constructor of a
    direct base class.

    --
    Ian Collins.
     
    Ian Collins, Mar 1, 2007
    #2
    1. Advertising

  3. Richard

    Richard Guest

    On Feb 28, 5:42 pm, Ian Collins <> wrote:
    > Richard wrote:
    > > If class C inherits from class B, which inherits from class A, is the
    > > order of the initializer list in the following constructor
    > > insignificant (C only inherits from A because it inherits from B)?:

    >
    > > C::C() :
    > > A(),
    > > B()
    > > {
    > > }

    >
    > > That is, would:

    >
    > > C::C() :
    > > B(),
    > > A()
    > > {
    > > }

    >
    > Neither of these should compile. You can only call the constructor of a
    > direct base class.
    >
    > --
    > Ian Collins.- Hide quoted text -
    >
    > - Show quoted text -


    It compiles for me. Something strange, here.
     
    Richard, Mar 1, 2007
    #3
  4. Richard

    Richard Guest

    On Feb 28, 6:30 pm, "Richard" <> wrote:
    > On Feb 28, 5:42 pm, Ian Collins <> wrote:
    >
    >
    >
    >
    >
    > > Richard wrote:
    > > > If class C inherits from class B, which inherits from class A, is the
    > > > order of the initializer list in the following constructor
    > > > insignificant (C only inherits from A because it inherits from B)?:

    >
    > > > C::C() :
    > > > A(),
    > > > B()
    > > > {
    > > > }

    >
    > > > That is, would:

    >
    > > > C::C() :
    > > > B(),
    > > > A()
    > > > {
    > > > }

    >
    > > Neither of these should compile. You can only call the constructor of a
    > > direct base class.

    >
    > > --
    > > Ian Collins.- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > It compiles for me. Something strange, here.- Hide quoted text -
    >
    > - Show quoted text -


    Ok, well something very close to this works in MULTI, but it doesn't
    seem to work in GCC.
     
    Richard, Mar 1, 2007
    #4
  5. Richard

    Ian Collins Guest

    Richard wrote:
    > On Feb 28, 5:42 pm, Ian Collins <> wrote:
    >
    >>Richard wrote:
    >>
    >>>If class C inherits from class B, which inherits from class A, is the
    >>>order of the initializer list in the following constructor
    >>>insignificant (C only inherits from A because it inherits from B)?:

    >>
    >>>C::C() :
    >>> A(),
    >>> B()
    >>>{
    >>>}

    >>
    >>>That is, would:

    >>
    >>>C::C() :
    >>> B(),
    >>> A()
    >>>{
    >>>}

    >>
    >>Neither of these should compile. You can only call the constructor of a
    >>direct base class.
    >>
    >>--
    >>Ian Collins.- Hide quoted text -
    >>
    >>- Show quoted text -

    >

    *Please trim the signature* that's the bit after the "-- ".
    >
    > It compiles for me. Something strange, here.
    >

    Well it shouldn't.

    Where did all that quoted text crap come from? I didn't write it.

    --
    Ian Collins.
     
    Ian Collins, Mar 1, 2007
    #5
  6. Richard

    Vallabha Guest

    On Mar 1, 6:29 am, "Richard" <> wrote:
    > If class C inherits from class B, which inherits from class A, is the
    > order of the initializer list in the following constructor
    > insignificant (C only inherits from A because it inherits from B)?:
    >
    > C::C() :
    > A(),
    > B()
    > {
    >
    > }
    >
    > That is, would:
    >
    > C::C() :
    > B(),
    > A()
    > {
    >
    > }
    >
    > work just the same? What if none of these were default constructors
    > and some arguments to C's constructor where passed to the parent class
    > constructors?
    >
    > Now, what if B's constructor was:
    >
    > B::B() :
    > A()
    > {
    >
    > }
    >
    > Upon invoking the constructor for C, would A's constructor be called
    > twice?


    I tried compiling your sample with g++ (3.3) and as expected it throws
    compilation error. Here is what it says:

    g++ const.cpp
    const.cpp: In constructor `C::C()':
    const.cpp:18: error: type `class A' is not a direct base of `C'

    Which compiler are you using?

    Cheers
    -Vallabha
    S7 Software Solutions
    http://s7solutions.com/
     
    Vallabha, Mar 1, 2007
    #6
  7. Richard

    Ron Natalie Guest

    Richard wrote:
    > If class C inherits from class B, which inherits from class A, is the
    > order of the initializer list in the following constructor
    > insignificant (C only inherits from A because it inherits from B)?:


    You can only provide initializers for the direct base classes and for
    any virtual base classes.

    The specification of mem initializers has no bearing on construction
    order. They just provide the arguments for the constructor when it
    would have been called anyway which is:

    1. From the most derived class, the virtual base classes as they
    appear in a depth-first, left-to-right, traversal.

    and then for each object recursively starting with the most derived
    are initialized:

    2. The non-virtual direct base classes are initialized in left to
    right order of their listing in the class definition (not the
    mem-initializers)

    3. The non-static members are initialized in order they appear in
    the class definition.

    4. The constructor body is run.
     
    Ron Natalie, Mar 1, 2007
    #7
  8. Richard

    Richard Guest

    It must be a compiler specific issue. I'm using the gbuild.exe
    compiler bundled with MULTI 4.2.3. It actually complains if I remove
    what is the equivalent of A's constructor from C's initializer list.
     
    Richard, Mar 2, 2007
    #8
  9. Richard

    Ian Collins Guest

    Richard wrote:
    > It must be a compiler specific issue. I'm using the gbuild.exe
    > compiler bundled with MULTI 4.2.3. It actually complains if I remove
    > what is the equivalent of A's constructor from C's initializer list.
    >

    File a bug.

    --
    Ian Collins.
     
    Ian Collins, Mar 2, 2007
    #9
    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. Dave Rudolf
    Replies:
    12
    Views:
    8,327
    Martijn Lievaart
    Feb 6, 2004
  2. Pmb
    Replies:
    2
    Views:
    408
  3. Replies:
    4
    Views:
    503
    Thorsten Ottosen
    Oct 14, 2004
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    425
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  5. Jeremy Smith
    Replies:
    2
    Views:
    602
    Jeremy Smith
    Aug 3, 2006
Loading...

Share This Page