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

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 ;
};

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.

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

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.

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?

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

This question has already been answered. Start a new discussion instead.