Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialization of non-integral type in initialization list

Reply
Thread Tools

Initialization of non-integral type in initialization list

 
 
anongroupaccount@googlemail.com
Guest
Posts: n/a
 
      12-11-2005
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?

 
Reply With Quote
 
 
 
 
anongroupaccount@googlemail.com
Guest
Posts: n/a
 
      12-11-2005

http://www.velocityreviews.com/forums/(E-Mail Removed) 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?

 
Reply With Quote
 
 
 
 
Neelesh Bodas
Guest
Posts: n/a
 
      12-11-2005
(E-Mail Removed) 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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      12-11-2005
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.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      12-11-2005
(E-Mail Removed) 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.
 
Reply With Quote
 
anongroupaccount@googlemail.com
Guest
Posts: n/a
 
      12-11-2005

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?

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      12-11-2005
(E-Mail Removed) 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
Why does list.__getitem__ return a list instance for subclasses ofthe list type? dackz Python 0 02-06-2007 04:44 PM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments