473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?

Mar 1 '07 #1
8 7173
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.
Mar 1 '07 #2
On Feb 28, 5:42 pm, Ian Collins <ian-n...@hotmail.comwrote:
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.

Mar 1 '07 #3
On Feb 28, 6:30 pm, "Richard" <rksa...@gmail.comwrote:
On Feb 28, 5:42 pm, Ian Collins <ian-n...@hotmail.comwrote:


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.

Mar 1 '07 #4
Richard wrote:
On Feb 28, 5:42 pm, Ian Collins <ian-n...@hotmail.comwrote:
>>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.
Mar 1 '07 #5
On Mar 1, 6:29 am, "Richard" <rksa...@gmail.comwrote:
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/

Mar 1 '07 #6
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.

Mar 1 '07 #7
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.

Mar 2 '07 #8
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.
Mar 2 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Alexander Stippler | last post by:
Hi, I wonder about the behaviour of como and icc on some very simple program. I thought initializing members of classes, which are of class type, would be 'direct initialized' (as the standard...
1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
4
by: Russell Silva | last post by:
I have a class A with a member variable type vector<foo> which is initialized upon construction (no standard () constructor): class A { protected: vector<foo> foo_vector; public: A(const...
12
by: Serve Laurijssen | last post by:
Is code like the following allowed? I am talking about the comma after the last function in the initializer. void f(void) {puts("f");} void g(void) {puts("g");} struct Funcs { void...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
5
by: Wu | last post by:
Hi there, I was wondering whether there is a way to use array initializer syntax to initialize a non-static array member in a class in C++. In particular, if I have a class Foo: class Foo {...
8
by: Jess | last post by:
Hello, When I define default constructors, I tend to use constructor initializers for member data. However, I was told the order in which members are initialized is determined by the order of...
10
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
5
by: Pallav singh | last post by:
How can we justify that initializer list is better in performance than assignment list in constructor of C++ ??
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.