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
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
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
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
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734