Initializer lists and multiple constructors

R

Richard

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

Ian Collins

Richard said:
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.
 
R

Richard

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.
 
I

Ian Collins

Richard said:
Neither of these should compile. You can only call the constructor of a
direct base class.
*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.
 
V

Vallabha

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/
 
R

Ron Natalie

Richard said:
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.
 
R

Richard

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.
 
I

Ian Collins

Richard said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top