|
|
 |
 |
 |
 |
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
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.
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/comp.lang.c++/browse_frm/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 ;) Regards, Zeppe
On May 29, 12:21 am, Zeppe
<zep_p@.remove.all.this.long.comment.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+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. 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
On May 28, 4:21 pm, Zeppe <zep_p@.remove.all.this.long.comment.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/comp.lang.c++/browse_frm/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:james.ka@gmail.com Conseils en informatique oriente objet/ Beratung in objektorientierter Datenverarbeitung 9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
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. 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
Thanks a lot! :) On May 29, 7:10 pm, Zeppe
<zep_p@.remove.all.this.long.comment.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+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
Jess wrote: > Thanks a lot! :) > On May 29, 7:10 pm, Zeppe > <zep_p@.remove.all.this.long.comment.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
On May 29, 8:50 pm, Zeppe
<zep_p@.remove.all.this.long.comment.yahoo.it> wrote: > Jess wrote: > > Thanks a lot! :) > > On May 29, 7:10 pm, Zeppe > > <zep_p@.remove.all.this.long.comment.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
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.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. >> [...] >> > 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/
|
 |
 |
 |
 |
|