Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Initializer lists and multiple constructors (http://www.velocityreviews.com/forums/t460804-initializer-lists-and-multiple-constructors.html)

Richard 03-01-2007 01:29 AM

Initializer lists and multiple constructors
 
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?


Ian Collins 03-01-2007 01:42 AM

Re: Initializer lists and multiple constructors
 
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.

Richard 03-01-2007 02:30 AM

Re: Initializer lists and multiple constructors
 
On Feb 28, 5:42 pm, Ian Collins <ian-n...@hotmail.com> 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 03-01-2007 02:51 AM

Re: Initializer lists and multiple constructors
 
On Feb 28, 6:30 pm, "Richard" <rksa...@gmail.com> wrote:
> On Feb 28, 5:42 pm, Ian Collins <ian-n...@hotmail.com> 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.


Ian Collins 03-01-2007 02:54 AM

Re: Initializer lists and multiple constructors
 
Richard wrote:
> On Feb 28, 5:42 pm, Ian Collins <ian-n...@hotmail.com> 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.

Vallabha 03-01-2007 05:28 AM

Re: Initializer lists and multiple constructors
 
On Mar 1, 6:29 am, "Richard" <rksa...@gmail.com> 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/


Ron Natalie 03-01-2007 02:10 PM

Re: Initializer lists and multiple constructors
 
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.


Richard 03-02-2007 12:30 AM

Re: Initializer lists and multiple constructors
 
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.


Ian Collins 03-02-2007 12:46 AM

Re: Initializer lists and multiple constructors
 
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.


All times are GMT. The time now is 01:05 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57