473,326 Members | 2,126 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,326 software developers and data experts.

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 8841
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.comment.yahoo.itwrote :
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+initialization 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.comment.yahoo.itwrote :
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 objektorientierter Datenverarbeitung
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+initialization 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.comment.yahoo.itwrote :
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+initialization 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.comment.yahoo.itwrote :
>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.comment.yahoo.itwrote :
Jess wrote:
Thanks a lot! :)
On May 29, 7:10 pm, Zeppe
<zep_p@.remove.all.this.long.comment.yahoo.itwrote :
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.itwrot e:
>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
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...
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>
14
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...
10
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...
18
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
by: desktop | last post by:
Why is this struct illegal: #include<iostream> struct debug { std::string d1 = "bob\n"; }; I get this error:
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?
8
by: aarklon | last post by:
Hi all, see:- http://linuxgazette.net/issue51/pramode.html
2
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.