473,480 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Ctor initialization lists: three details beyond the FAQ-lite

Hi!

A) Why isn't it possible to set a member of the BASE class in
an initialization list of a DERIVED class constructor (except for
'calling' the base constructor from there, of course)? I even
tried prefixing them with BASE:: but to no avail. Still it's
ok when I set them in the construtor body, not the init list.
Does this mean that it's a small advantage of the initialization
inside the constructor over initialization in the init list
(though I know the init lists are 'cleaner' in general).

B) Is it, on the other hand, possible (looks to me like it is)
to use the values of BASE class members to initialize DERIVED
class members in the initialization list?

C) I know the initialization lists do not support the syntax
to initialize structs all at once, but why they don't allow
to initialize struct *members* like in
foo::foo() : bar.x(0), bar.y(0) {};
is beyond me. Why aren't members of structs treated like
'ordinary variables'? I know it's easiest to add a constructor
to the struct and to use it in the init list, but I'm just
curious...

TIA,
- J.
Jul 22 '05 #1
1 4141
On Mon, 19 Apr 2004, Jacek Dziedzic wrote:
Hi!

A) Why isn't it possible to set a member of the BASE class in
an initialization list of a DERIVED class constructor (except for
'calling' the base constructor from there, of course)? I even
First, note that initializing the base by calling its constructor, and
initializing the base by initializing its members, is mutually
exclusive.

Second, the constructor of a derived class cannot, in general,
initialize every member of the base class, since the derived class does
not have access to private members of its base.

Third, the base class constructor establishes the class invariant of the
base class. If the constructor of a derived class could directly
initialize members of its base, it would need to assume this task in
addition to that of establishing its own invariant. But it is not
generally in a position to do so without having access to all members of
the base.

In terms of construction, a base class behaves similar to an aggregated
class, and this seems a good idea since it enhances encapsulation.

tried prefixing them with BASE:: but to no avail. Still it's
ok when I set them in the construtor body, not the init list.
Does this mean that it's a small advantage of the initialization
inside the constructor over initialization in the init list
(though I know the init lists are 'cleaner' in general).
There is no initialization inside the constructor's body.

B) Is it, on the other hand, possible (looks to me like it is)
to use the values of BASE class members to initialize DERIVED
class members in the initialization list?
Yes, since the base class constructor has been called before any member
of the derived class is initialized.

C) I know the initialization lists do not support the syntax
to initialize structs all at once, but why they don't allow
to initialize struct *members* like in
foo::foo() : bar.x(0), bar.y(0) {};
is beyond me.
The initialization list allows you to initialize the class members. The
only way to initialize a member struct (i.e. class) is by calling its
constructor.
Why aren't members of structs treated like
'ordinary variables'? I know it's easiest to add a constructor
to the struct and to use it in the init list, but I'm just
curious...

TIA,
- J.


--
Claudio Jolowicz


Jul 22 '05 #2

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

Similar topics

9
3731
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
41
3897
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
7
2508
by: Razzie | last post by:
Hello all, This may seem like a silly question but I suddenly wondered about this. Is there any difference between the following 2 situations? 1) class A { private SomeObject o = new...
5
3011
by: PasalicZaharije | last post by:
Hallo, few days ago I see ctor like this: Ctor() try : v1(0) { // some code } catch(...) { // some code }
4
1868
by: Jacek Dziedzic | last post by:
Hello! Suppose I have a class Foo that defines a default c'tor that initializes some data using an initialization list: Foo::Foo() : member1(0), member2(0), member3(NULL), member4(20) // and...
5
1605
by: Grahamo | last post by:
Hi, I have a basic question regarding some legacy code I'm working with; Basically the code looks something like this. I'd like to know if there are any reasons why a particular approach is...
10
8497
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
3
3367
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and...
51
8554
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
4
3690
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
0
6904
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...
0
7032
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
7076
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...
1
6730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
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,...
0
4471
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...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
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 ...

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.