Initialization Lists

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 125
Reputation: bala24 is an unknown quantity at this point 
Solved Threads: 11
bala24's Avatar
bala24 bala24 is offline Offline
Junior Poster

Initialization Lists

 
0
  #1
Jun 13th, 2007
Hi,

I was just browsing through some stuff about Initialization lists in C++ and got stuck with a doubt..

Why is it necessary to use initialization lists while giving a value to constants or references ??
Why is it not possible to do so in the normal way..i.e initializing them within the constructor.

Any help would be appreciated..

Thanks in advance..
Michelangelo

"The best place to find a helping hand is at the end of your own arm"
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Initialization Lists

 
0
  #2
Jun 13th, 2007
members or base class sub-objects cannot be initialized inside the body of the constructor; they can only be assigned to. initialization is always before the body of the constructor executes. if not initialized expliciltly using colon initialization, the implementation will provide default initialization if available. for any member which does not have default initialization (constants and references are examples of these), an explicit initialization would have to be provided.
  1. struct A
  2. {
  3. A()
  4. // error 1: must_be_initialized must be initialized
  5. {
  6. must_be_initialized = 0 ; // error 2: constant can't be assigned to
  7. }
  8. const int must_be_initialized ;
  9. };
Last edited by vijayan121; Jun 13th, 2007 at 6:05 am.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 7
Reputation: bsrivastava is an unknown quantity at this point 
Solved Threads: 1
bsrivastava bsrivastava is offline Offline
Newbie Poster

Re: Initialization Lists

 
0
  #3
Jun 14th, 2007
Originally Posted by bala24 View Post
Hi,

I was just browsing through some stuff about Initialization lists in C++ and got stuck with a doubt..

Why is it necessary to use initialization lists while giving a value to constants or references ??
Why is it not possible to do so in the normal way..i.e initializing them within the constructor.

Any help would be appreciated..

Thanks in advance..

Classes can contain member objects of class type, but to ensure that initialization requirements for
the member objects are met, one of the following conditions must be met:

The contained object’s class requires no constructor.
The contained object’s class has an accessible default constructor.
The containing class’s constructors all explicitly initialize the contained object.
The following example shows how to perform such an initialization:

// Declare a class CPoint.
class CPoint
{
public:
CPoint( int x, int y ) { _x = x; _y = y; }
private:
int _x, _y;
};

// Declare a CRectangleangle class that contains objects of type CPoint.
class CRectangle
{
public:
CRectangle( int x1, int y1, int x2, int y2 );
private:
CPoint TopLeft, BottomRight;
};

// Define the constructor for class CRectangle. This constructor
// explicitly initializes the objects of type CPoint.
CRectangle::CRectangle( int x1, int y1, int x2, int y2 ) :
TopLeft( x1, y1 ), BottomRight( x2, y2 )
{
}
The CRectangle class, shown in the preceding example, contains two member objects of class CPoint. Its
constructor explicitly initializes the objects TopLeft and BottomRight. Note that a colon follows
the closing parenthesis of the constructor (in the definition). The colon is followed by the member
names and arguments with which to initialize the objects of type CPoint.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Initialization Lists

 
0
  #4
Jun 14th, 2007
Originally Posted by vijayan121 View Post
members or base class sub-objects cannot be initialized inside the body of the constructor; they can only be assigned to. initialization is always before the body of the constructor executes.
Think that answers the question adequately.
Just to be clearer.. the difference between initialization and assignment is:
1. Initialization is done at the time of (or along with) creation of the variable. (which happens only once). If this is not done it's initialized with SOME value anyway.
2. Assignment can happen anytime.

Going by the normal C/C++ tip:
do
int i = 0;
instead of:
int i;
i = 0;


So when you know the value for a variable at the time of it's creation it's better to initialize it to save a li'l time, rather than letting SOME value being set in initialization and then assigning it.

Finally initialization is done in the "initialization list" of c'tor. And assignment happens inside the c'tor (or anywhere for that matter).
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 125
Reputation: bala24 is an unknown quantity at this point 
Solved Threads: 11
bala24's Avatar
bala24 bala24 is offline Offline
Junior Poster

Re: Initialization Lists

 
0
  #5
Jun 14th, 2007
Thanks for that extensive explanation..
Now i get it..
Michelangelo

"The best place to find a helping hand is at the end of your own arm"
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Initialization Lists

 
0
  #6
Jun 14th, 2007
Originally Posted by thekashyap View Post
Going by the normal C/C++ tip:
do
int i = 0;
instead of:
int i;
i = 0;


So when you know the value for a variable at the time of it's creation it's better to initialize it to save a li'l time, rather than letting SOME value being set in initialization and then assigning it.
AFAIK, this is not the case. When a variable is declared, a block of memory is reserved for it and thats it. No default initialization is done. Its because of this we get junk values when we try using uninitialized variables. This value is interpreted based on the contents of those memory location and the type of variable under consideration.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Initialization Lists

 
0
  #7
Jun 14th, 2007
Originally Posted by ~s.o.s~ View Post
AFAIK, this is not the case. When a variable is declared, a block of memory is reserved for it and thats it.
You are right. That's true AFAIK.
But also a static variables is always guaranteed to be initialized.
Also there is a clear difference between basic and non basic types. For a non-basic type the c'tor is always called (after memory allocation). Where as for a basic type this doesnt' seem to be true.
One thing I donno is whether allocating it statically or dynamically make a difference? Don't have a compiler handy so may be someone can check if possible. This is what I'm talking abt:
  1. int i ;
  2. int *j = new int ;
  3. //printf( i & *j )
  4. //Will both i and *j print junk values?
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Initialization Lists

 
0
  #8
Jun 14th, 2007
> For a non-basic type the c'tor is always called (after memory allocation).
Though you have to realize that classes in the end are composed of primitive types and if an explicit constructor is not written to take care of the initialization, the same situation would result (junk values of primitive types).

> Will both i and *j print junk values?
No, the one dynamically allocated would be given the default value which is zero for integer types and '\0' for char types.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC