Re: initializing static class variables
From: John Harrison (john_andronicus_at_hotmail.com)
Date: 08/01/04
- Next message: Simon G Best: "Re: Yet another Attempt at Disproving the Halting Problem"
- Previous message: John Harrison: "Re: How Methods Return Objects"
- In reply to: Mark : "Re: initializing static class variables"
- Next in thread: Mark : "Re: initializing static class variables"
- Reply: Mark : "Re: initializing static class variables"
- Reply: Neil Zanella: "Re: initializing static class variables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 01 Aug 2004 07:36:46 +0100
On Sat, 31 Jul 2004 23:28:10 -0400, Mark <mark_netNews@hotmail.com> wrote:
> On Sat, 31 Jul 2004 20:12:43 +0100, "John Harrison"
> <john_andronicus@hotmail.com> wrote:
>
>
>> I believe it was introduced in 2003 standard but I can't be sure since I
>> don't have the 1998 standard available.
>>
>> I guess the difference is best illustrated with an example
>>
>> struct Foo
>> {
>> int x;
>> };
>>
>> The standard says (8.5 para 5) that if Foo is default initialised then
>> its
>> default constructor is called. This is obviously an implicit default
>> constructor and 12.1 para 7 and 12.6.2 para 4 together say that x is not
>> initialised in this case (i.e. it has an undefined value).
>>
>> Hoever 8.5 para 5 says that if Foo is value initialised then because Foo
>> does not have a user-declared constructor each of its members is value
>> initialised. And when a scalar type is value initalsed it is in fact
>> zero
>> initialised.
>>
> [....]
>>
>> But I'm still learning this stuff myself, so watch out I could easily be
>> wrong.
>>
>> john
>
> Default initilization when viewed from the 1998 and 03 version of the
> standard seem contradictory.
>
> The 1998 standard for C++ (ISO/IEC 14882-1998) says:
>
> "An object whose initializer is an empty set of parentheses, i.e., (),
> shall be default-initialized."
That's directly contradicted by the 2003 standard which says value
initialised.
>
> A little before that, it says:
>
> "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 storage for the object is zero-initialized."
>
> (Section 8.5, Initializers)
>
> So given
>
> struct data_t
> {
> int Idx;
> int Jdx;
> int Kdx;
> } data;
>
> data_t type is a POD class type (plain old data), so with empty
> parentheses, it should be zero-initialized.
Assuming you are talking about the 1998 standard I don't think so. Look up
the definition of compiler generated default constructor. I think you will
see that it says Idx, Jdx and Kdx remain uninitialised not zero
initialised.
>
> data_t() is not exactly a constructor call. It denotes a temporary
> object of type data_t, constructed with default initialization.
>
> So data(data_t()) initializes the object named "data" with a
> temporary, default-initialized object of its type, and for this type
> default initialization is zero-initialization.
>
> Note that some compilers may not do this right. In any event, it
> seems to me the 03 version of the standard refers to this as value
> initilization.
And that's the crucial difference, because value initialisation does not
use the compiler generated default constructor.
john
- Next message: Simon G Best: "Re: Yet another Attempt at Disproving the Halting Problem"
- Previous message: John Harrison: "Re: How Methods Return Objects"
- In reply to: Mark : "Re: initializing static class variables"
- Next in thread: Mark : "Re: initializing static class variables"
- Reply: Mark : "Re: initializing static class variables"
- Reply: Neil Zanella: "Re: initializing static class variables"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|