| | |
Initialization Lists
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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..
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"
"The best place to find a helping hand is at the end of your own arm"
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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.
C++ Syntax (Toggle Plain Text)
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 ; };
Last edited by vijayan121; Jun 13th, 2007 at 6:05 am.
•
•
Join Date: May 2005
Posts: 7
Reputation:
Solved Threads: 1
•
•
•
•
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.
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.. ?
•
•
•
•
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.
I don't accept change; I don't deserve to live.
•
•
•
•
AFAIK, this is not the case. When a variable is declared, a block of memory is reserved for it and thats it.

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:
c Syntax (Toggle Plain Text)
int i ; int *j = new int ; //printf( i & *j ) //Will both i and *j print junk values?
Are you Agile.. ?
> 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.
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.
![]() |
Similar Threads
- problem dealing with constructor (C++)
- Problem with inheritance (C++)
- Class Constructor Shorthand (C++)
Other Threads in the C++ Forum
- Previous Thread: Handle right clicks
- Next Thread: Real-world programs?
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






