Jess said:
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.
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.
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