Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initializer lists and multiple constructors

Reply
Thread Tools

Initializer lists and multiple constructors

 
 
Richard
Guest
Posts: n/a
 
      03-01-2007
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?

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-01-2007
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.
 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      03-01-2007
On Feb 28, 5:42 pm, Ian Collins <(E-Mail Removed)> 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.

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      03-01-2007
On Feb 28, 6:30 pm, "Richard" <(E-Mail Removed)> wrote:
> On Feb 28, 5:42 pm, Ian Collins <(E-Mail Removed)> 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.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-01-2007
Richard wrote:
> On Feb 28, 5:42 pm, Ian Collins <(E-Mail Removed)> 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.
 
Reply With Quote
 
Vallabha
Guest
Posts: n/a
 
      03-01-2007
On Mar 1, 6:29 am, "Richard" <(E-Mail Removed)> 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/

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      03-01-2007
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.

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      03-02-2007
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.

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      03-02-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg constructors/copy constructors inheritance srp113 C++ 3 02-17-2009 04:01 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments