S
Steven T. Hatton
I mistakenly set this to the comp.std.c++ a few days back. I don't believe
it passed the moderator's veto - and I did not expect or desire anything
different. But the question remains:
ISO/IEC 14882:2003(E) §8.5 says:
To zero-initialize an object of type T means:
5
-- 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 zero-
initialized;
-- 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.
I take this to mean that pointers are by default zero-initialized. Is this
a correct understanding? Would you rely on this to be the case when you
construct a class with pointer members? I will probably always initialize
all pointer explicitly, but I'm not sure what I should expect if I don't.
My observations have been that they don't always behave as I expect when I
don't initialize them explicitly.
I've found it surprizingly difficult to find a direct discussion of this
topic in the resources I have available.
it passed the moderator's veto - and I did not expect or desire anything
different. But the question remains:
ISO/IEC 14882:2003(E) §8.5 says:
To zero-initialize an object of type T means:
5
-- 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 zero-
initialized;
-- 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.
I take this to mean that pointers are by default zero-initialized. Is this
a correct understanding? Would you rely on this to be the case when you
construct a class with pointer members? I will probably always initialize
all pointer explicitly, but I'm not sure what I should expect if I don't.
My observations have been that they don't always behave as I expect when I
don't initialize them explicitly.
I've found it surprizingly difficult to find a direct discussion of this
topic in the resources I have available.