Static Variable Problems

C

Connell Gauld

Hi,
I'm using VC++.
I have a class called exClass defined in exClass.h
exClass has a static variable, declared like this in the public section.
static int exStaticVar;
Below the class definition I have initialised (right word?) it like this
int exClass::nextFree;

Now below is a code fragment from a non-static function in another class
(exClass.h was included at the top of the .cpp file).

exClass * otherClass::doFunction(args)
{
if (condition)
{
for(loop_stuff)
{
if(condition)
{
// The line below works fine.
someFunction(exClass.exStaticVar);

// The line below does not.
exClass.exStaticVar++;
}

return gModel;
}
} else {
return 0;
}
}

The error caused is below and suggests that it cannot find exClass which
it must be able to do since it did it in the function.
error C2143: syntax error : missing ';' before '.'

Using VC++.

Any help greatly appreciated,
Connell
 
J

John Harrison

Connell Gauld said:
MADE A MISTAKE, SORRY, CORRECTED BELOW



NOT THIS
BUT THIS

int exClass::exStaticVar

exClass::exStaticVar

in both places. exClass.exStaticVar is wrong both times.

john
 
C

Connell Gauld

MADE A MISTAKE, SORRY, CORRECTED BELOW

Connell said:
Hi,
I'm using VC++.
I have a class called exClass defined in exClass.h
exClass has a static variable, declared like this in the public section.
static int exStaticVar;
Below the class definition I have initialised (right word?) it like this

NOT THIS
int exClass::nextFree;
BUT THIS

int exClass::exStaticVar
 
J

JKop

Header file:

Class Blah
{
public:
static WhateverClass super_blah;
};


SOURCE file:

WhateverClass Blah::super_blah(87,234,21.3);


If you stick the definition into the header file, then
you'll have a mutliple definition if more than one source
file includes it.

Rule of thumb: If you've got a static member, you're going
to need a source file, even if it's only going to be 1 line
long.


-JKop
 
G

Gottfried Eibner

Connell said:
Hi,
I'm using VC++.
I have a class called exClass defined in exClass.h
exClass has a static variable, declared like this in the public section.
static int exStaticVar;
Below the class definition I have initialised (right word?) it like this
int exClass::nextFree;
No,

class exClass { static int staticVar; ... } // is reffered as definition
(header file)
int exClass::staticVar; // is reffered as declaration or instantiation
(implementation file)
int exClass::staticVar=0;// declared and initialised at the same time!

To your problem in the code:
exClass * otherClass::doFunction(args)
{
...
if(condition)
{
// The line below works fine.
someFunction(exClass.exStaticVar);

// The line below does not.
exClass.exStaticVar++;
}
...
}
If you use exClass.*, you mean a instance of class exClass with the name
exClass, i.e. like:
int nameOfInstance; // int is the type and nameOfInstance the name of an
instance of type int;
exClass exClass; // the first exClass is the type and the second
would be the instance.

So, don't mix up instance names and class names. If you want to use the
static variable of a class, you have to refere to it via its namespace:
AnyClass::staticVariableName.
In this way you can access the variable anywhere in your code.
If you use it in class-function, you can leave the AnyClass:: referring
scheme.

So in your code just use staticVar (since you use it in the implementation
part of your class).

You can also access static vars of an class via an instance:

exClass instanceOfExClass;
instanceOfExClass.staticVar =0;

But keep in mind, that it is a static variable and any change of staticVar
is visible to all instances.
Static functions and variables do not need any instance of the class (by the
way).

So use exClass::staticVar of just staticVar in your code.

I do not know your definition of someFunction and why it works in the first
place. But it's strange code anyway.

regards,
Gottfried
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,479
Latest member
JaysonK723

Latest Threads

Top