473,473 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Initialization of non-integral type in initialization list

class CustomType
{
public:
CustomType(){_i = 0;}
CustomType(int i) : _i(i) {}
private:
int _i;
};

class MyClass
{
public:
MyClass() : _member(4){}
private:
CustomType _member;
};

Will _member be ever created before the initialization list in MyClass?
Am I guarantueed that it will only get created that once?

I have run some tests, but I can't tell if my results are
implementation specific, or undefined behaviour. Is this the right way?

Dec 11 '05 #1
6 2148

an**************@googlemail.com wrote:
class CustomType
{
public:
CustomType(){_i = 0;}
CustomType(int i) : _i(i) {}
private:
int _i;
};

class MyClass
{
public:
MyClass() : _member(4){}
private:
CustomType _member;
};

Will _member be ever created before the initialization list in MyClass?
Am I guarantueed that it will only get created that once?

I have run some tests, but I can't tell if my results are
implementation specific, or undefined behaviour. Is this the right way?


Sorry for all of the questions today. I've been writing a lot of
programs to fill up grey areas in my knowledge.

A further question: Imagine CustomType is an ABC. There's no way I can
initialize that in the initialization list, is there? I'd actually have
to use a pointer in this case, and assign it a value from making a new
concrete class down the inheritance chain, right?

Dec 11 '05 #2
an**************@googlemail.com wrote:
class CustomType
{
public:
CustomType(){_i = 0;}
Use Constructor initialization list
CustomType() : _i(0) { } ;
CustomType(int i) : _i(i) {}
private:
int _i;
};

class MyClass
{
public:
MyClass() : _member(4){}
private:
CustomType _member;
};
Donot use names starting with an underscore - they are reserved for
implementation.
Will _member be ever created before the initialization list in MyClass?
Am I guarantueed that it will only get created that once?

the initialization list will initialize the member "_member" when
constructor of the class CustomType is called. Space for _member is
reserved when you instantiate the class. This is immediately followed
by the constructor call to initialize the object. So , though the
object creation and initialization take place at two distinct points in
time, as far as the programmer is concerned, they are "atomic".

Also, if a member is of a user-defined type and it is not initialized
in the constructor initialization list, then the compiler calls the
default constructor for that member before executing the code of the
constructor.


A further question: Imagine CustomType is an ABC. There's no way I can
initialize that in the initialization list, is there? I'd actually have
to use a pointer in this case, and assign it a value from making a new
concrete class down the inheritance chain, right?


If CustomType is an absract base class, you cannot have an instance of
CustomType as a member of MyClass. So you are right in pointing out
that you would actually need a pointer there.

HTH.

Dec 11 '05 #3
Neelesh Bodas wrote:
Donot use names starting with an underscore - they are reserved for
implementation.


No they aren't. Only ones begining with underscore followed by an
uppercase letter, or those in the global namespace. His member
variables here are fine as far as the language is concerned.
Dec 11 '05 #4
an**************@googlemail.com wrote:

Will _member be ever created before the initialization list in MyClass?
Am I guarantueed that it will only get created that once?

The initialization list is just a indication of the initializers that
are used when the appropriate initialization occurs. It has no bearing
on the order of initialization, regardless of the order of the
inializers in the list, or by their absence.

Construction always occurs in a definite order:

1. Any virtual base classes are initialized for the most derived object.

then

2. Recursively, the any non-virtual bases are initialized, in the
order they are declared in the class definition(i.e., after the
colon that follows the class name, NOT the member initializer list).

3. All non-static members are initialized in the order they are listed
in the class definition.

4. The body of the constructor is run.

All the initializer list does is say that when that constructor is used,
then those parameters are used to initialize the listed subobject when
the time for that initialization occurs as specified above.
Dec 11 '05 #5

Ron Natalie wrote:
Neelesh Bodas wrote:
Donot use names starting with an underscore - they are reserved for
implementation.


No they aren't. Only ones begining with underscore followed by an
uppercase letter, or those in the global namespace. His member
variables here are fine as far as the language is concerned.


Strangely, almost all of the C++ code I've seen uses an underscore in
private variables. Is this not actually a common thing to do?

Dec 11 '05 #6
an**************@googlemail.com wrote:
Ron Natalie wrote:
Neelesh Bodas wrote:
Donot use names starting with an underscore - they are reserved for
implementation.

No they aren't. Only ones begining with underscore followed by an
uppercase letter, or those in the global namespace. His member
variables here are fine as far as the language is concerned.


Strangely, almost all of the C++ code I've seen uses an underscore in
private variables. Is this not actually a common thing to do?

A lot of people use a trailing underscore. I tend to do neither.
For me a well designed class doesn't require any special decoration
on the member variables. Member variables aren't visible outside
the class, and a class member function rarely touches a variable
outside the class, so what's the point?
Dec 11 '05 #7

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

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
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
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
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
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...
1
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...
0
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
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.