473,769 Members | 6,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2525
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 objektorientier ter Datenverarbeitu ng
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.co m>, Jeff Schwab
<je**@schwabcen ter.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

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

Similar topics

8
2971
by: Jinesh | last post by:
I illustrate the compiler error I get using the following example. --------------------------------------------------------------- Class ClassName { private: static const int constVarName = 100; void functionName(int parameterName) }; void ClassName::functionName(int parameterName=constVarName)
3
3614
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 non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a DLL by another external object file (not within the DLL). It only occurs when the static const...
8
4589
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: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
14
2871
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
15011
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 error/warning: "error: invalid use of nonstatic data member " However, he did NOT mention this error in the book explicitly.It happens always in the constructor when you try to initialize some data members in the constructor and try to accsess other data...
8
2048
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 holding to the theory? Pls explain. Here is an example: namespace StaticNonStaticMembers { class Program {
9
8895
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 *defined* "x"'s value to be 10, isn't above statement a
15
7871
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
3777
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 void UseMap (const std::string &key, int value)
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.