static/nonstatic data member declaration/definition

J

Jeffrey

My understanding is that if you write

class X {
int y;
static int z;
};

then you've defined (and declared) X and y, but you have only declared
(and not defined) z. If you'd like to actually define z, you also
need to add

int X::z;

Can anybody tell me the reason that the language was designed to be
like this? It seems it would be simpler if z were defined in the same
way as y, so presumably there's some good reason.
 
R

Rolf Magnus

Jeffrey said:
My understanding is that if you write

class X {
int y;
static int z;
};

then you've defined (and declared) X and y, but you have only declared
(and not defined) z. If you'd like to actually define z, you also
need to add

int X::z;

Can anybody tell me the reason that the language was designed to be
like this?

Well, whenever you instantiate the class, you get the member y. But z is
supposed to exist exactly noce, not once for every object.
 
J

James Kanze

My understanding is that if you write
class X {
int y;
static int z;
};
then you've defined (and declared) X and y, but you have only
declared (and not defined) z. If you'd like to actually
define z, you also need to add
int X::z;
Can anybody tell me the reason that the language was designed
to be like this? It seems it would be simpler if z were
defined in the same way as y, so presumably there's some good
reason.

Mainly historical reasons, I suspect, but of course, if there is
an initializer (as there usually should be), you usually don't
want it in a header.
 
J

Juha Nieminen

James said:
Mainly historical reasons, I suspect, but of course, if there is
an initializer (as there usually should be), you usually don't
want it in a header.

Aren't static variables always initialized to 0 (or null), even
without a specific initialization?
 
J

Juha Nieminen

Jeff said:
Btw, there's a loop-hole for class templates. Static member variables
of class templates (not including explicit specializations) can live
right up in the header file, along with the rest of the corresponding
template definition.

Personally I see no reason why this should be supported for templates
and *not* for non-templates. What would be the reason for the latter?

Btw, the next standard will allow specifying initial values for member
variables in the variable definitions (so that you don't have to
initialize them explicitly in the constructor), ie:

class A
{
int i = 5, j = 10;
};

Will this extend to static member variables as well?
 
A

Andrey Tarasevich

Jeffrey said:
My understanding is that if you write

class X {
int y;
static int z;
};

then you've defined (and declared) X
Yes.

and y,

No. You only _declared_ 'y' as a member of class 'X'. Non-static data
members of classes don't get [independently] _defined_ in C++ at all.
The notion is simply not applicable here.

From the less formal point of view, the purpose of defining a data
entity is to associate a storage location with it. For non-static data
members the storage is assigned when (and where) the complete object is
defined.
but you have only declared
(and not defined) z.

Same as with 'y' or any other data member.
If you'd like to actually define z, you also
need to add

int X::z;

Yes. And you have to do it in one and only one translation unit.
Can anybody tell me the reason that the language was designed to be
like this?

When you define something that has a location in storage, the compiler
normally wants to know which translation unit this definition should be
associated with. The responsibility of choosing the translation unit is
delegated to you. This is what really hides behind the need to define it.
It seems it would be simpler if z were defined in the same
way as y, so presumably there's some good reason.

Firstly, you assumption that 'y' is "defined" by class definition alone
is incorrect. It isn't.

Secondly, the "definition" if 'y' (in the "storage allocation" sense)
can happen the way it happens specifically because it is a non-static
data member of the class. It can't apply to 'z'.
 
J

James Kanze

Aren't static variables always initialized to 0 (or null),
even without a specific initialization?

Variables with static lifetime are "zero initialized", but that
is of limited use. You don't really want to require that all
constants be 0.
 
J

James Kanze

No. You only _declared_ 'y' as a member of class 'X'.

According to the standard, this is a definition.
Non-static data members of classes don't get [independently]
_defined_ in C++ at all. The notion is simply not applicable
here.

It is according to the standard. It may not be the most
intuitive use of language, but it works on the whole: a
declaration which is not a definition requires a definition
somewhere, which is not the case here.
From the less formal point of view, the purpose of defining a
data entity is to associate a storage location with it.

Which holds here. The compiler allocates storage for y in X.

[...]
When you define something that has a location in storage, the
compiler normally wants to know which translation unit this
definition should be associated with. The responsibility of
choosing the translation unit is delegated to you. This is
what really hides behind the need to define it.

What really hides behind it is history. The necessary
mechanisms are needed in order to handle the case for templates.
They weren't present in the past, however.
Firstly, you assumption that 'y' is "defined" by class
definition alone is incorrect. It isn't.

It is according to the standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top