I'm very new with C++ and I'm trying to understand the difference between a declaration and a definition.
As far as I understand, a declaration tells the compiler that an identifier exists and a definition assigns memory (not a value) to it. Am I right? :).
I'd like to know if this statement is a declaration or a definition:
static int x;
and why a static variable inside a class is a declaration and not definition:
struct x {
static int y;
};
Thanks for advance and I hope you can help me, I have been searching information in google but I really want to be sure.
Kind regards.
You're close, you're only off when you say "not a value" -- a definition of an object usually provides the initial value as well as the storage. The automatic scalar variables are an exception -- their initial value may be left indeterminate.
To quote the standard,
all but one of the following are definitions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int a; // defines a
externconstint c = 1; // defines c
int f(int x) { return x+a; } // defines f and defines x
struct S { int a; int b; }; // defines S, S::a, and S::b
struct X { // defines X
int x; // defines non-static data member x
staticint y; // declares static data member y
X(): x(0) { } // defines a constructor of X
};
int X::y = 1; // defines X::y
enum { up, down }; // defines up and down
namespace N { int d; } // defines N and N::d
namespace N1 = N; // defines N1
X anX; // defines anX
whereas these are just declarations:
1 2 3 4 5 6 7
externint a; // declares a
externconstint c; // declares c
int f(int); // declares f
struct S; // declares S
typedefint Int; // declares Int
extern X anotherX; // declares anotherX
using N::d; // declares d
Your non-member static int x; is a definition of an object of type int, with static storage duration, and the initial value zero.
A static data member is declared inside the class body: you're telling the compiler that somewhere out there exists this static int x::y, which every single instance of your struct x will access.
And then in that one file where you want to actually place it, you define it, outside the class body.
You're close, you're only off when you say "not a value" -- a definition of an object usually provides the initial value as well as the storage. The automatic scalar variables are an exception -- their initial value may be left indeterminate.
I didn't mean that it is not a definition if an assignment is made, I meant that if an assignment is not made, it's a definition anyway, because what defines a definition is the assignment of memory, not the value.
Your non-member static int x; is a definition of an object of type int, with static storage duration, and the initial value zero.
That is what I thought, it's a definition :).
A static data member is declared inside the class body: you're telling the compiler that somewhere out there exists this static int x::y, which every single instance of your struct x will access.
And then in that one file where you want to actually place it, you define it, outside the class body.