PDA

View Full Version : Static field and public method



probine
5th March 2006, 11:07
For some reason this code doesn't work

class Media
{
private:
static int number;

public:
void addMediaType();
};

void Media :: addMediaType()
{
number=1;
}

How can I refer to my static number from my public method ?

wysota
5th March 2006, 12:02
How can I refer to my static number from my public method ?

Media::number = 1;

But you have to initialise it somewhere first. The initialisation should take place outside of any class member and should be done only once, something like that:


//header
class X{
static int var;
};

//implementation
int X::var = 1;