473,772 Members | 3,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7188
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.co mwrote:
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.co mwrote:


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.co mwrote:
>>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
insignifican t (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
3017
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 says). But in the example below the copy constructor of Vec is executed when initializing the Ref object. Is this standard conform? Doesn't it result in bad performance? #include <iostream>
1
2495
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 I found no satisfactory answer yet. It is about composed/aggregated classes. I am developing a scientific code (Monte Carlo) in which I find it natural to have classes with several levels of aggregation.
4
1889
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 vector<foo>& init_foo_vector); }
12
5169
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 (*f)(void); }; struct Funcs funcs = {
9
2372
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 class, if the class has more than than one cosntructor, by making sure that each constructor has a different signature. I have managed to learn that and get
5
2296
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 { double arr; }
8
2446
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 declaration in the class, instead of the order they appear in the constructor initializer. This would introduce interdependencies. Therefore, it is safer to avoid such interdependence by assigning values to these members inside the constructor...
10
2743
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
5
8202
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
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.