Topic: Initialization of statics...
Author: adk@Warren.MENTORG.COM (Ajay Kamdar)
Date: 8 Feb 1993 17:34:56 -0500 Raw View
In article <1993Feb8.173907.8527@src.dec.com> detlefs@src.dec.com (Dave Detlefs) writes:
>
>I happened to notice a citation of the ARM in a message from Fergus James
>Henderson:
>
> According to ARM 18.3, allowing the definition to be omitted
> (with initialization defaulting to all zeroes) is an anachronism, and
> "A C++ implementation is not obliged to provide these features [the
> anachronisms".
>
>I looked it up, and section 18.3 does indeed say that. However, this
>seems to conflict with section 8.4, where it says
>
> Variables with storage class static (s. 3.5) that are not
> initialized are guaranteed to start off as 0 converted to the
> appropriate type. So are members of static class objects.
>
>So is default-initialization-of-statics-to-0 a feature or an
>anachronism. I'm going to assume for the the nonce that its an
>anachronism that was just overlooked.
>
What the ARM is saying is that you can omit the initialization, but
not the definition. To also omit the definition is an anachronism.
==== a.h ====
class A {
static int d_data1;
static int d_data2;
};
=== a.c ===
// at file scope
int A::d_data1 = 5; // defining and initializing d_data1.
int A::d_data2; // defining d_data2, but not initializing it.
// C++ guarantees that d_data2 will be initialized to 0
// at startup. This is a feature.
//
// Many current implementations let you omit this
// definition altogether. That is an anachronism.
//
// With newer C++ implementations, not providing this
// definition may result in an undefined symbol when
// linking the executable.
--
I speak for none but myself.
Ajay Kamdar Email : ajay_kamdar@mentorg.com
Mentor Graphics, IC Group (Warren, NJ) Phone : (908) 604-0842
Author: detlefs@src.dec.com (Dave Detlefs)
Date: Mon, 8 Feb 93 17:39:07 GMT Raw View
I happened to notice a citation of the ARM in a message from Fergus James
Henderson:
According to ARM 18.3, allowing the definition to be omitted
(with initialization defaulting to all zeroes) is an anachronism, and
"A C++ implementation is not obliged to provide these features [the
anachronisms".
I looked it up, and section 18.3 does indeed say that. However, this
seems to conflict with section 8.4, where it says
Variables with storage class static (s. 3.5) that are not
initialized are guaranteed to start off as 0 converted to the
appropriate type. So are members of static class objects.
So is default-initialization-of-statics-to-0 a feature or an
anachronism. I'm going to assume for the the nonce that its an
anachronism that was just overlooked.
Dave
Author: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
Date: Tue, 9 Feb 1993 13:02:30 GMT Raw View
In article <1993Feb8.173907.8527@src.dec.com> detlefs@src.dec.com (Dave Detlefs) writes:
>
>I happened to notice a citation of the ARM in a message from Fergus James
>Henderson:
>
> According to ARM 18.3, allowing the definition to be omitted
> (with initialization defaulting to all zeroes) is an anachronism, and
> "A C++ implementation is not obliged to provide these features [the
> anachronisms".
>
>I looked it up, and section 18.3 does indeed say that. However, this
>seems to conflict with section 8.4, where it says
>
> Variables with storage class static (s. 3.5) that are not
> initialized are guaranteed to start off as 0 converted to the
> appropriate type. So are members of static class objects.
>
>So is default-initialization-of-statics-to-0 a feature or an
>anachronism. I'm going to assume for the the nonce that its an
>anachronism that was just overlooked.
>
You must supply a defintion, you need not initialise it.
If you dont, it gets 0, or, for class types, it gets the
default constructor.
For a class static, you have to write out
the defintion:
class X { static int i; };
int X::i; // inited to 0, def required here.
I think that for a simple variable:
extern int j; // declaration only
int j=0; // definition
int j; // also a definition.
(is this right?)
BTW:I'm looking at allowing:
class X {
static int j=0; // definition!
};
Doesn't seem too much hassle ....
--
;----------------------------------------------------------------------
JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
;------ SCIENTIFIC AND ENGINEERING SOFTWARE ---ph: 2 799 8223 --------