Is there a reason you would initialise variables like this?
-
Hi all, Is there any particular reason you would initialise private members of a class like this...
SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { }
As opposed to this?SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; }
From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, MarkMark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen
-
Hi all, Is there any particular reason you would initialise private members of a class like this...
SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { }
As opposed to this?SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; }
From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, MarkMark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen
Const and reference members can only be initialized in an intializer list. Moreover if we don't initialize the variables in the initializers list the compiler will have to do it for us and later on in the constructor this will be initialized again. Hence, there would be two calls for variable initialization
You talk about Being HUMAN. I have it in my name AnsHUMAN
-
Hi all, Is there any particular reason you would initialise private members of a class like this...
SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { }
As opposed to this?SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; }
From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, MarkMark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen
It's just a matter of style, although I think the first option is preferred. MSDN has this to say[^] on the matter. You may also like to check if Bjarne Stroustrup has more to offer.
One of these days I'm going to think of a really clever signature.
-
Const and reference members can only be initialized in an intializer list. Moreover if we don't initialize the variables in the initializers list the compiler will have to do it for us and later on in the constructor this will be initialized again. Hence, there would be two calls for variable initialization
You talk about Being HUMAN. I have it in my name AnsHUMAN
_AnsHUMAN_ wrote:
Moreover if we don't initialize the variables in the initializers list the compiler will have to do it for us and later on in the constructor this will be initialized again.
I doubt that. The easiest way for a compiler to provide default values is to do the following - Request memory block for the object - Overwrite the entire block with zeros. - Continue the construction process. Providing code to set the default value for each member is thus not required. Any other initialization, beyond the default value, would occur regardless.
-
Hi all, Is there any particular reason you would initialise private members of a class like this...
SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { }
As opposed to this?SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; }
From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, MarkMark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen
C++ FAQ by Marshall Cline says: [10.6] Should my constructors use "initialization lists" or "assignment"? [^].
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Hi all, Is there any particular reason you would initialise private members of a class like this...
SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { }
As opposed to this?SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; }
From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, MarkMark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen
// Is there any particular reason Yes :) :
class A
{
//..
public:
A();
A(int);
A& operator=(int);
};class B
{
A m_a;
public:
B() { m_a = 3; /*second call, after A::A()*/}
// B() : m_a(3) /*first and last call of A::A(int)*/ {}
};They sought it with thimbles, they sought it with care; They pursued it with forks and hope; They threatened its life with a railway-share; They charmed it with smiles and soap. :)
-
_AnsHUMAN_ wrote:
Moreover if we don't initialize the variables in the initializers list the compiler will have to do it for us and later on in the constructor this will be initialized again.
I doubt that. The easiest way for a compiler to provide default values is to do the following - Request memory block for the object - Overwrite the entire block with zeros. - Continue the construction process. Providing code to set the default value for each member is thus not required. Any other initialization, beyond the default value, would occur regardless.
In standard C++, member without default constructor are not initialized to 0 automatically but are random.
Philippe Mori
-
Hi all, Is there any particular reason you would initialise private members of a class like this...
SomeClass::Constructor(void): privateVar1(NULL), privateVar2(true) { }
As opposed to this?SomeClass::Constructor(void) { privateVar1 = NULL; privateVar2 = true; }
From what I understand the first instance would be useful when inheritance is involved.. Perhaps this is just a favored style? Cheers, MarkMark Brock "We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen
The difference is that, in the first way of initialization (known as 'Initialization List'), the constructors of the initialized members are called. While in the second option, all the objects get constructed before reaching the first executable statement of the constructor. Hence in the second way of initialization, the assignment operators gets called.
[Delegates] [Virtual Desktop] [Tray Me !]
-Malli...! :rose:**** -
C++ FAQ by Marshall Cline says: [10.6] Should my constructors use "initialization lists" or "assignment"? [^].
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
_AnsHUMAN_ wrote:
Moreover if we don't initialize the variables in the initializers list the compiler will have to do it for us and later on in the constructor this will be initialized again.
I doubt that. The easiest way for a compiler to provide default values is to do the following - Request memory block for the object - Overwrite the entire block with zeros. - Continue the construction process. Providing code to set the default value for each member is thus not required. Any other initialization, beyond the default value, would occur regardless.
Some compilers initialize memory to zero. But for compatibility reasons, the C++ standard leaves (built-in type) members untouched (I don't know the latest one). Of course, if your members have default constructors, they will be called (before the constructor body) unless you place them in the initializer list.
-
C++ FAQ by Marshall Cline says: [10.6] Should my constructors use "initialization lists" or "assignment"? [^].
"We make a living by what we get, we make a life by what we give." --Winston Churchill
...and don't forget that the compiler processes the initializer list following the declaration order given in the class definition. So you better stick to that order in the list (to prevent confusion). There are tools availabe that check this (e.g. cppcheck)
-
In standard C++, member without default constructor are not initialized to 0 automatically but are random.
Philippe Mori
Philippe Mori wrote:
In standard C++, member without default constructor are not initialized to 0 automatically but are random.
Wrong. Section 4.9.5 C++ specification clearly states that class member variables have a default value if they do not have an initializer.
class Any
{
....
int var1 = 2; // This is an initializer.
int var2; // No initializer so set to default value (zero.)
}Local variables (those declared in a method) have not such default value.
-
Some compilers initialize memory to zero. But for compatibility reasons, the C++ standard leaves (built-in type) members untouched (I don't know the latest one). Of course, if your members have default constructors, they will be called (before the constructor body) unless you place them in the initializer list.
w-peuker wrote:
Some compilers initialize memory to zero. But for compatibility reasons, the C++ standard leaves (built-in type) members untouched (I don't know the latest one).
Wrong. The only compilers that leave data members non-initialized would be compilers that are not implementing the ANSI C++ specification. Section 4.9.5 C++ specification clearly states that class member variables have a default value if they do not have an initializer.
class Any
{
....
int var1 = 2; // This is an initializer.
int var2; // No initializer so set to default value (zero.)
}This happens regardless of how construction occurs.
-
Philippe Mori wrote:
In standard C++, member without default constructor are not initialized to 0 automatically but are random.
Wrong. Section 4.9.5 C++ specification clearly states that class member variables have a default value if they do not have an initializer.
class Any
{
....
int var1 = 2; // This is an initializer.
int var2; // No initializer so set to default value (zero.)
}Local variables (those declared in a method) have not such default value.
At this time, I program mainly in C# and in that case meber are initialized to (as member of a ref class would also be initialized in C++/CLI). As far as I can tell, you have misunderstood the standard. A static member will be initialized to 0 but not a regiular member in the general case. I have found many link that confirm what I have said. http://www.cplusplus.com/forum/general/17582/[^] http://stackoverflow.com/questions/1069621/are-members-of-a-c-struct-initialized-to-0-by-default[^] http://stackoverflow.com/questions/2614809/what-is-the-default-value-for-c-class-members[^] http://msdn.microsoft.com/en-us/library/s0wk5dy9.aspx[^]
Philippe Mori
-
w-peuker wrote:
Some compilers initialize memory to zero. But for compatibility reasons, the C++ standard leaves (built-in type) members untouched (I don't know the latest one).
Wrong. The only compilers that leave data members non-initialized would be compilers that are not implementing the ANSI C++ specification. Section 4.9.5 C++ specification clearly states that class member variables have a default value if they do not have an initializer.
class Any
{
....
int var1 = 2; // This is an initializer.
int var2; // No initializer so set to default value (zero.)
}This happens regardless of how construction occurs.
See my answer above. You have misunderstood the standard. I have found many links that proove otherwise. By the way, your example is not even valid C++ code.
Philippe Mori
-
At this time, I program mainly in C# and in that case meber are initialized to (as member of a ref class would also be initialized in C++/CLI). As far as I can tell, you have misunderstood the standard. A static member will be initialized to 0 but not a regiular member in the general case. I have found many link that confirm what I have said. http://www.cplusplus.com/forum/general/17582/[^] http://stackoverflow.com/questions/1069621/are-members-of-a-c-struct-initialized-to-0-by-default[^] http://stackoverflow.com/questions/2614809/what-is-the-default-value-for-c-class-members[^] http://msdn.microsoft.com/en-us/library/s0wk5dy9.aspx[^]
Philippe Mori
-
See my answer above. You have misunderstood the standard. I have found many links that proove otherwise. By the way, your example is not even valid C++ code.
Philippe Mori