
please wait
|
|
8.5/5: To zero-initialize an object of type T means: — if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T; — if T is a non-union class type, each nonstatic data member and each base-class subobject is zeroinitialized; — if T is a union type, the object’s first named data member89) is zero-initialized; — if T is an array type, each element is zero-initialized; — if T is a reference type, no initialization is performed. To default-initialize an object of type T means: — if T is a non-POD class type (clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); — if T is an array type, each element is default-initialized; — otherwise, the object is zero-initialized. To value-initialize an object of type T means: — if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor); — if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized; — if T is an array type, then each element is value-initialized; — otherwise, the object is zero-initialized. |
T x;
requests default initialization, which is a volatile. The line T x = T();
requests value initialization, but MAY invoke the copy constructor (although it didn't in gnu gcc). The line T x();
declares a function with no parameters and return type T. So, how can I value-initialize without fear that the copy constructor may be invoked. Of course, if the type has copy constructor, then I probably don't need to value-initialize in the first place, but suppose I don't know that. I mean, suppose I am using somebody else's code in some company and the code may change over time and I don't want to rely on anything.
|
|
NodPod a = NonPod( )
Framework wrote: |
---|
Replace your code with this: |
Your're trying to call the class/struct's default constructor manually which is disallowed. |
int i = int()
.Also, if you don't initialize a class/struct member, then it holds "garbage" such as this: 21982368. |
Actually, if I don't initialize anything with explicit call to its default constructor, if it doesn't even have default constructor, then it will hold garbage. This is what default initialization means in my opinion. |
May be. But it does compile with gcc. Is this gcc specific? After all, I can use int i = int(). |
Do you mean that it would be more proper to always use default initialization? |
Framework wrote: |
---|
I've tried printing the value of an uninitialized class/struct member and it gave me "garbage" until I initialized it with a value. Try it yourself. |
S x;
leaves the members uninitialized, and S x = S();
initializes them. On the other hand if S is not a simple structure and has a copy and default constructor, then S x = S();
may (or may not) invoke the copy constructor. But I can not use S x();
to avoid the copy constructor for certain, because this is a function declaration.PanGalactic wrote: |
---|
You also need to be aware of what an aggregate is. |
if S is some simple structure with no constructor, then S x; leaves the members uninitialized, and S x = S(); initializes them. On the other hand if S is not a simple structure and has a copy and default constructor, then S x = S(); may (or may not) invoke the copy constructor. |
|
|
PanGalactic wrote: |
---|
This should help. It has one of the most concise explanations I have read. http://en.wikipedia.org/wiki/C%2B%2B_classes#Differences_between_struct_and_classes_in_C.2B.2B |
simeonz wrote: |
---|
What other programming languages do you recommend if I decide to switch from C++? |
jsmith wrote: |
---|
In your original example, NonPod's value member will be initialized to zero no matter what syntax you use to default construct it. |
Pod's value member will be uninitialized (read: could be anything) no matter what syntax you use to default construct it. |
PanGalactic wrote: |
---|
Well, no one should know just one language. I like Python as a good compliment to C++. |
C++ Python ASM Lisp Java |
ne555 wrote: |
---|
I don't see in your quote any form that a variable could have garbage. About the c.value, don't forget that garbage could be 0, and your compiler likes to optimize things. |
Is there a 03 standard? I thought that the 98 was the last one. |