954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Initialization Lists

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..

bala24
Junior Poster
125 posts since Oct 2006
Reputation Points: 15
Solved Threads: 11
 

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.

struct A
{
  A() 
     // error 1: must_be_initialized must be initialized
  {
    must_be_initialized = 0 ; // error 2: constant can't be assigned to
  }
  const int must_be_initialized ;
};
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 
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.

bsrivastava
Newbie Poster
7 posts since May 2005
Reputation Points: 11
Solved Threads: 1
 
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).

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

Thanks for that extensive explanation..
Now i get it..

bala24
Junior Poster
125 posts since Oct 2006
Reputation Points: 15
Solved Threads: 11
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
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:

int i ;
int *j = new int ;
//printf( i & *j )
//Will both i and *j print junk values?
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

> 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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You