473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

static/nonstatic data member declaration/definition

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.
Sep 29 '08 #1
10 2495
Jeffrey wrote:
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.
Because you only want the definition to be in one translation unit, or
you'll get link errors. The declarations, on the other hand, should be
in every translation unit that needs access to them.
Sep 29 '08 #2
Jeff Schwab wrote:
Jeffrey wrote:
>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.

Because you only want the definition to be in one translation unit, or
you'll get link errors. The declarations, on the other hand, should be
in every translation unit that needs access to them.
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.
Sep 29 '08 #3
Jeffrey wrote:
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.

Sep 29 '08 #4
On Sep 29, 4:40 am, Jeffrey <jkar...@gmail.comwrote:
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 29 '08 #5
James Kanze wrote:
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?
Sep 29 '08 #6
Jeff Schwab wrote:
>Because you only want the definition to be in one translation unit, or
you'll get link errors. The declarations, on the other hand, should
be in every translation unit that needs access to them.

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?
Sep 29 '08 #7
Jeffrey wrote:
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'.

--
Best regards,
Andrey Tarasevich
Sep 29 '08 #8
Juha Nieminen wrote:
Jeff Schwab wrote:
>>Because you only want the definition to be in one translation unit, or
you'll get link errors. The declarations, on the other hand, should
be in every translation unit that needs access to them.
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?
I don't believe so. The point of the in-class member initialization (I
would guess) is to let multiple constructors leverage common
member-initialization code, rather than all defining
similar-but-different initializer lists. The same reasoning doesn't
really apply to static members, which aren't initialized in
constructors' initializer lists at all.

AFAIK, this is the latest version of the proposal:
http://www.open-std.org/jtc1/sc22/wg...008/n2712.html
Sep 29 '08 #9
In article <Pu******************************@giganews.com>, Jeff Schwab
<je**@schwabcenter.comwrote:
Jeffrey wrote:
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.

Because you only want the definition to be in one translation unit, or
you'll get link errors.
That's just begging the question.
Sep 29 '08 #10
On Sep 29, 7:41 pm, Juha Nieminen <nos...@thanks.invalidwrote:
James Kanze wrote:
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?
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Sep 30 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jinesh | last post by:
I illustrate the compiler error I get using the following example. --------------------------------------------------------------- Class ClassName { private: static const int constVarName = 100;...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
14
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
7
by: The|Godfather | last post by:
Hi everybody, I read Scotte Meyer's "Effective C++" book twice and I know that he mentioned something specific about constructors and destructors that was related to the following...
8
by: Rajesh | last post by:
Based on my understanding static members do not have access to non- static members. In the below example static method 'name' accessing non-static method creating reference. Seems to me it is not...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
5
by: chgans | last post by:
Hi all, I'm having difficulties with some template static member, especially when this member is a template instance, for example: ---- template<typename T> class BaseT { public: static...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.