473,603 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static class constant, declaration or definition?

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
definition? In addition, I was told if I'd like to make a definition
for A::x, then I should do:

const int A::x;

This is more confusing, since the code above seems to be redefining
the value of "x", but it's supposed to be a constant hence its value
shouldn't be changed.

Can someone please tell me what is going on there?

Thanks,
Jess

May 28 '07 #1
9 8872
Jess wrote:
>
const int A::x;

This is more confusing, since the code above seems to be redefining
the value of "x", but it's supposed to be a constant hence its value
shouldn't be changed.

Can someone please tell me what is going on there?

Thanks,
Jess
A::x is in a different namespace.
May 28 '07 #2
Jess wrote:
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.
yes, is a declaration with initialization.
As I've *defined* "x"'s value to be 10, isn't above statement a
definition?
You don't define a variable to have a certain value. You define a
variable. When you define a static variable, that variable will have a
space in memory assigned to that. Otherwise, it won't, and the linker
will complain. Anyway, the declaration is enough for some inline
substitutions done at compile time, for which the variable address is
not required.

It happens that this issue has been discussed a short time ago:
http://groups.google.co.uk/group/com...ea37970364ab76
In addition, I was told if I'd like to make a definition
for A::x, then I should do:

const int A::x;

This is more confusing, since the code above seems to be redefining
the value of "x", but it's supposed to be a constant hence its value
shouldn't be changed.
You are just defining once the x member variable. Do not confuse
definition with initialization ;)

Regards,

Zeppe

May 28 '07 #3
On May 29, 12:21 am, Zeppe
<zep_p@.remove. all.this.long.c omment.yahoo.it wrote:
Jess wrote:
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.

yes, is a declaration with initialization.
As I've *defined* "x"'s value to be 10, isn't above statement a
definition?

You don't define a variable to have a certain value. You define a
variable. When you define a static variable, that variable will have a
space in memory assigned to that. Otherwise, it won't, and the linker
will complain. Anyway, the declaration is enough for some inline
substitutions done at compile time, for which the variable address is
not required.
If I have

int x;

then I'm defining variable "x", is this right? so if I have

int x = 10;

then I think I'm defining variable "x" and initializing it to 10, is
this right? If so, why the "static const int x = 10" isn't a
definition+init ialization of "x"? I guess I'm confused by the
differences (especially syntactical differences) between definition
and declaration. To me, declaration means "declaring" something
without giving its value, e.g. void f(); is a declaration but not a
definition. However, for objects of built-in types or user-defined
types, I'm not quite sure the differences between declaration and
definition. From the examples above, it seems definitions look
similar to declarations. If a compiler only allocates memory to
defined objects but not to declared objects, then there must be some
syntactical differences.
It happens that this issue has been discussed a short time ago:http://groups.google.co.uk/group/com...rm/thread/3040...
Thanks for pointing out, I see I also need to define the variable in
the .cpp file.
In addition, I was told if I'd like to make a definition
for A::x, then I should do:
>
const int A::x;
>
This is more confusing, since the code above seems to be redefining
the value of "x", but it's supposed to be a constant hence its value
shouldn't be changed.

You are just defining once the x member variable. Do not confuse
definition with initialization ;)
So this is my problem, why is the second one a definition, while the
first one a declaration? They look almost identical except the
'static' keyword, and the second one doesn't even give an init value
to 'x'.

Thanks,
Jess
May 29 '07 #4
On May 28, 4:21 pm, Zeppe
<zep_p@.remove. all.this.long.c omment.yahoo.it wrote:
Jess wrote:
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.
yes, is a declaration with initialization.
Sort of. In a very concrete sense, a declaration cannot
initialize, because it doesn't create anything to be
initialized. But the C++ allows this special case, where you
specify the initialization in a declaration.
As I've *defined* "x"'s value to be 10, isn't above statement a
definition?
You don't define a variable to have a certain value. You define a
variable. When you define a static variable, that variable will have a
space in memory assigned to that. Otherwise, it won't, and the linker
will complain. Anyway, the declaration is enough for some inline
substitutions done at compile time, for which the variable address is
not required.
It happens that this issue has been discussed a short time
ago:http://groups.google.co.uk/group/com...rm/thread/3040....
In addition, I was told if I'd like to make a definition
for A::x, then I should do:
const int A::x;
This is more confusing, since the code above seems to be redefining
the value of "x", but it's supposed to be a constant hence its value
shouldn't be changed.
You are just defining once the x member variable. Do not confuse
definition with initialization ;)
They're not totally unrelated. The actual "initialization " of
the variable will only be "generated" in the definition, even
though the value used in the initialization will be taken from
the declaration, and not the definition. This code is in all
respects the equivalent of:

class A
{
static int const x ;
} ;

int const A::x = 10 ;

with the one exception that the initialization value is visible
in all translation units which contain the class definition.

--
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

May 29 '07 #5
Jess wrote:
If I have

int x;

then I'm defining variable "x", is this right? so if I have

int x = 10;

then I think I'm defining variable "x" and initializing it to 10, is
this right?
all right.
If so, why the "static const int x = 10" isn't a
definition+init ialization of "x"? I guess I'm confused by the
differences (especially syntactical differences) between definition
and declaration. To me, declaration means "declaring" something
without giving its value, e.g. void f(); is a declaration but not a
definition.
it's not a matter of value, but a matter of specification, the most of
the time. As you said, the "int x;" is a definition, and you are not
giving x a value, you are just saying, in english "there is a variable
here, his name is x, and it's an int.". This is a definition.
To make the point, let's say that a declaration is when you tell the
program that something exists somewhere. A definition, on the other
side, is when you tell the program that something exists, and it is
here, and you specify the exact behaviour of it. A definition is also a
declaration.

The syntax for the definitions changes for different entities. For
example, for a variable, a definition is

int x;

and a declaration is

extern int x; /* (a variable named x whose name is x exists, but is not
here, it's somewhere in the program) */

for a function, as you said, a definition is:

int f(){ /*...*/ }

and a declaration is

int f();

for a class (that is, a type, not a variable), a definition is

class Stack {
public:
// ...
};
/* a type named Stack exists, its behaviour is this, and it's here */
and a declaration is

class Stack;
/* somewhere there is a type that is a class and whose name is Stack */

For a static member variable, when you say
class Foo{
static const int x;
};

it's "this type, that is defined here, uses a variable, that is a class
variable (static), it's const, it's an int, its name is x, and is
somewhere in the program". You still have to tell the program where is
this variable... So, what about the initialization, you might think?
Well, that's because usually those static const variables are used as
defines, they cant be changed, and sometimes their value can be
substituted at compilation time. So, they have introduced this extension
to give them the value in the declaration (that for a class member has
to be unique) to help the compiler. This is a sort of exception to the
rule that if you give a value to something, that's also the definition.
From the examples above, it seems definitions look
similar to declarations. If a compiler only allocates memory to
defined objects but not to declared objects, then there must be some
syntactical differences.
The meaning of the symbols depend on the context:

static const int x = 10;

is a definition + initialization, but

class Foo{
static const int x = 10;
};

just declares the class member x;

The class variables are associated to the class, not to the specific
instances. Now, when you define a class, you are not defining any
instance (no memory is associated to the type, just a behaviour). When
you are defining class variables, you are defining the instances of that
type. But the global variables, they have to be defined somewhere as well.
>
>It happens that this issue has been discussed a short time ago:http://groups.google.co.uk/group/com...rm/thread/3040...

Thanks for pointing out, I see I also need to define the variable in
the .cpp file.
That is the only definition of the variable.
> In addition, I was told if I'd like to make a definition
for A::x, then I should do:

const int A::x;

This is more confusing, since the code above seems to be redefining
the value of "x", but it's supposed to be a constant hence its value
shouldn't be changed.

You are just defining once the x member variable. Do not confuse
definition with initialization ;)

So this is my problem, why is the second one a definition, while the
first one a declaration? They look almost identical except the
'static' keyword, and the second one doesn't even give an init value
to 'x'.
As I said, look at the context. The first is inside of a class
definition: you are defining a type, and you are stating that it uses a
"shared" variable x, that is int and const (and eventually will have
also a value), but you are not specifying where this variable will be.
In the second case you are specifying the variable, but you won't give
that a value because you already did ni the declaration (that can be a
little confusing, I have to say ^^)

Regards,

Zeppe
May 29 '07 #6
Thanks a lot! :)

On May 29, 7:10 pm, Zeppe
<zep_p@.remove. all.this.long.c omment.yahoo.it wrote:
Jess wrote:
If I have
int x;
then I'm defining variable "x", is this right? so if I have
int x = 10;
then I think I'm defining variable "x" and initializing it to 10, is
this right?

all right.
If so, why the "static const int x = 10" isn't a
definition+init ialization of "x"? I guess I'm confused by the
differences (especially syntactical differences) between definition
and declaration. To me, declaration means "declaring" something
without giving its value, e.g. void f(); is a declaration but not a
definition.

it's not a matter of value, but a matter of specification, the most of
the time. As you said, the "int x;" is a definition, and you are not
giving x a value, you are just saying, in english "there is a variable
here, his name is x, and it's an int.". This is a definition.
To make the point, let's say that a declaration is when you tell the
program that something exists somewhere. A definition, on the other
side, is when you tell the program that something exists, and it is
here, and you specify the exact behaviour of it. A definition is also a
declaration.

The syntax for the definitions changes for different entities. For
example, for a variable, a definition is

int x;

and a declaration is

extern int x; /* (a variable named x whose name is x exists, but is not
here, it's somewhere in the program) */
In the file that I define this "extern" int x (say in f1.cpp), is the
following the correct way to define it?

extern int x = 10;

In other words, is this "extern" necessary? Moreover, if another .cpp
wishes to use this "x", it should declare it by "extern int x". Does
this file still need to include the header of f1.cpp?
As for the class members, can I say we only have declarations rather
than definitions for those member data, because compiler doesn't
allocate memory when it sees class definitions?

Thanks,
Jess

May 29 '07 #7
Jess wrote:
Thanks a lot! :)

On May 29, 7:10 pm, Zeppe
<zep_p@.remove. all.this.long.c omment.yahoo.it wrote:
>int x;

and a declaration is

extern int x; /* (a variable named x whose name is x exists, but is not
here, it's somewhere in the program) */

In the file that I define this "extern" int x (say in f1.cpp), is the
following the correct way to define it?

extern int x = 10;
if you define x, you don't need extern. for the static variables (not
member variables), extern is actually needed to _declare_ a variable,
not to define it. Moreover, the initialization in the declaration is
valid only for certain types of const static member variables in a
class, so "extern int i = 10;" is wrong. Either "int i = 10;"
(definition) or "extern int i" (declaration).
Moreover, if another .cpp
wishes to use this "x", it should declare it by "extern int x".
true.
Does
this file still need to include the header of f1.cpp?
not at all.
As for the class members, can I say we only have declarations rather
than definitions for those member data, because compiler doesn't
allocate memory when it sees class definitions?
Not sure I have understood your question. When you define a class (i.e.,
class Foo { /* behaviour*/ };), you are actually not allocating any
memory, you are defining the structure of a type. Of course, all the
instances of that type (that is, the variables of type Foo) will have
the same structure, and when you define them the memory will be
allocated and they will be localized somewhere in the program (where you
are defining them). But what about the static data member? Where shall
they be localized? You have to provide a separate definition for them.
That's because the definition of a type (the class) can not be also a
definition for the variables that are used inside.

Hope it has clarified a little bit more :)

Regards,

Zeppe
May 29 '07 #8
On May 29, 8:50 pm, Zeppe
<zep_p@.remove. all.this.long.c omment.yahoo.it wrote:
Jess wrote:
Thanks a lot! :)
On May 29, 7:10 pm, Zeppe
<zep_p@.remove. all.this.long.c omment.yahoo.it wrote:
int x;
and a declaration is
extern int x; /* (a variable named x whose name is x exists, but is not
here, it's somewhere in the program) */
In the file that I define this "extern" int x (say in f1.cpp), is the
following the correct way to define it?
extern int x = 10;

if you define x, you don't need extern. for the static variables (not
member variables), extern is actually needed to _declare_ a variable,
not to define it. Moreover, the initialization in the declaration is
valid only for certain types of const static member variables in a
class, so "extern int i = 10;" is wrong. Either "int i = 10;"
(definition) or "extern int i" (declaration).
Moreover, if another .cpp
wishes to use this "x", it should declare it by "extern int x".

true.
Does
this file still need to include the header of f1.cpp?

not at all.
As for the class members, can I say we only have declarations rather
than definitions for those member data, because compiler doesn't
allocate memory when it sees class definitions?

Not sure I have understood your question. When you define a class (i.e.,
class Foo { /* behaviour*/ };), you are actually not allocating any
memory, you are defining the structure of a type. Of course, all the
instances of that type (that is, the variables of type Foo) will have
the same structure, and when you define them the memory will be
allocated and they will be localized somewhere in the program (where you
are defining them). But what about the static data member? Where shall
they be localized? You have to provide a separate definition for them.
That's because the definition of a type (the class) can not be also a
definition for the variables that are used inside.

Hope it has clarified a little bit more :)

Regards,

Zeppe
Yes, that answers my questions, thanks. :)
Jess

May 29 '07 #9
On 29 May 2007 01:35:30 -0700, James Kanze wrote:
>On May 28, 4:21 pm, Zeppe
<zep_p@.remove .all.this.long. comment.yahoo.i twrote:
>Jess wrote:
> 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.
>[...]
> In addition, I was told if I'd like to make a definition
for A::x, then I should do:
> const int A::x;
>[...]

They're not totally unrelated. The actual "initialization " of
the variable will only be "generated" in the definition, even
though the value used in the initialization will be taken from
the declaration, and not the definition. This code is in all
respects the equivalent of:

class A
{
static int const x ;
} ;

int const A::x = 10 ;

with the one exception that the initialization value is visible
in all translation units which contain the class definition.
And it is a common *misconception* that the form you give prevents in
itself using x as an integral constant expression.

I think we can agree, anyway, that the in-class initialization is a
hack to cope with traditional linkers.

--
Gennaro Prota -- C++ Developer, For Hire
https://sourceforge.net/projects/breeze/
May 29 '07 #10

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

Similar topics

4
5414
by: cppsks | last post by:
"Defining static const variables inside the class is not universally supported yet, so for now I guess you'll have to move the definition out of the body of the class. No, static const inside classes is only allowed for integral consts, like static const enum and static const int, not for arrays and structs. It does make sense for an array of integral consts though." I read the above statements in this group a while back. First off,...
14
2853
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>
14
2808
by: John Ratliff | last post by:
I'm trying to find out whether g++ has a bug or not. Wait, don't leave, it's a standard C++ question, I promise. This program will compile and link fine under mingw/g++ 3.4.2, but fails to link under Linux/g++ 3.3.3. --------------------------------------------- #include <iostream> #include <utility>
10
2642
by: stonny | last post by:
I read the following sentence from a c++ website, but can not understand why. can anyone help me with it? " An important detail to keep in mind when debugging or implementing a program using a static class member is that you cannot initialize the static class member inside of the class. In fact, if you decide to put your code in a header file, you cannot even initialize the static variable inside of the header file; do it in a .cpp file...
18
5632
by: mati | last post by:
Hi The following code works: #include <vector> class C { private: static const int m_static = 2; public: void f(const std::vector<int>& v)
5
5606
by: desktop | last post by:
Why is this struct illegal: #include<iostream> struct debug { std::string d1 = "bob\n"; }; I get this error:
15
7849
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?
8
5702
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
2
2473
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
0
7996
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
7928
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,...
1
8060
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8273
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5878
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
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3951
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.