Connecting Tech Pros Worldwide Forums | Help | Site Map

static class constant, declaration or definition? (2)

Newbie
 
Join Date: Jan 2008
Posts: 1
#1: Jan 31 '08
I've read this thread (couldn't do a post):

http://www.thescripts.com/forum/thread653679.html

How is this called:

CClass SomeInstance

Is it a definition? I assume it is because memory is allocated, however according to the info at the previous thread:
Expand|Select|Wrap|Line Numbers
  1. class CClass{
  2. int A;
  3. void funcB;
  4. };
  5.  
Is a (class) definition also. Or should it now called a definition of an object of class CClass?

And:
Expand|Select|Wrap|Line Numbers
  1. int A;
  2. void funcB;
  3.  
What are these? Are the data and method members both declarations since there isn't any memory allocated (it's inside a class definition)?

And I assume the same story goes (declaring and initializing, but it should be defined somewhere else) for non-consts statics (so leaving out the consts in the next piece of code)?

Expand|Select|Wrap|Line Numbers
  1. class A{
  2. static const int x = 10;
  3. };
  4.  
Expand|Select|Wrap|Line Numbers
  1. const int A::x;
  2.  
BTW: Is it required to define such non-const static? Because I can imagine that compilation will not give any errors (didn't test it, however). In that way it would be defined implicit - containing garbage?

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,370
#2: Jan 31 '08

re: static class constant, declaration or definition? (2)


CClass SomeInstance

is a definition.

A definition requires memory storage.
Expand|Select|Wrap|Line Numbers
  1. class CClass{
  2. int A;
  3. void funcB;
  4. };
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1. int A;
  2. void funcB;
  3.  
are declarations. No memory is allocated.

Now you are getting sneaky:
Expand|Select|Wrap|Line Numbers
  1. class A{
  2. static const int x = 10;
  3. };
  4.  
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:
Expand|Select|Wrap|Line Numbers
  1. class A{
  2. static const int x;
  3. };
  4.  
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:
Expand|Select|Wrap|Line Numbers
  1. const int A::x;
  2.  
won't compile since a const needs a value when defined. You have to do this:
Expand|Select|Wrap|Line Numbers
  1. const int A::x = 10;
  2.  
//////////////////////////////////////////////////////////////////////
OK this is a separate issue.

1) A const by default is static (internal linkage when defined)
Expand|Select|Wrap|Line Numbers
  1. const int x = 10;
  2.  
That means in another file you cannot:
Expand|Select|Wrap|Line Numbers
  1. extern const int x;
  2. cout << x;
  3.  
unless you define the const a sharable (external linkage):
Expand|Select|Wrap|Line Numbers
  1. extern const int x = 10;
  2.  
then you can:
Expand|Select|Wrap|Line Numbers
  1. extern const in x;
  2. cout << x;
  3.  
This problem still exists:
Expand|Select|Wrap|Line Numbers
  1. const int x = 10;
  2.  
If you do this in a header file, you define an int x every time you include the header. The header file should contain:
Expand|Select|Wrap|Line Numbers
  1. extern const int x;
  2.  
which is a declaration and not a definition. Then in one source file you define a single const int x:
Expand|Select|Wrap|Line Numbers
  1. extern const int x = 10;
  2.  
That way there is only one int x in the program.
Reply