CClass SomeInstance
is a definition.
A definition requires memory storage.
-
class CClass{
-
int A;
-
void funcB;
-
};
-
is a declaration. No memory required. Your previous thread is in error in calling this a definition. This is a common mistake caused by poor discipline in using programming terminology.
These members inside the class:
are declarations. No memory is allocated.
Now you are getting sneaky:
-
class A{
-
static const int x = 10;
-
};
-
This is a definition since a static variable with a value of 10 is created. This is allowed since the default linkage for a const is internal. That is, the variable cannot be used ourside the file where it is defined. The problem with this is that if this class is included in 500 source files, then you have 500 ints with a value of 10. One in each file. Please don't do this.
Instead dothis:
-
class A{
-
static const int x;
-
};
-
is a declaration because the static variable is not created. Here you have to create this variable somewhere else as a global variable. Then all A objects will use this single variable.
This:
won't compile since a const needs a value when defined. You have to do this:
//////////////////////////////////////////////////////////////////////
OK this is a separate issue.
1) A const by default is static (internal linkage when defined)
That means in another file you cannot:
-
extern const int x;
-
cout << x;
-
unless you define the const a sharable (external linkage):
then you can:
-
extern const in x;
-
cout << x;
-
This problem still exists:
If you do this in a header file, you define an int x every time you include the header. The header file should contain:
which is a declaration and not a definition. Then in one source file you define a single const int x:
That way there is only one int x in the program.