static variables in derived classes

A

Andrew Bullock

Hi,


I have a super class, and several sub classes which all extend the super.

I want to add a static variable to the super class so that it exists in
all its sub classes, how do i do this?

All my attempts have resulted in symbol referencing errors.

Do i declare the variable in the super? If so, how, and then how do i
access it from the sub classes, and also from outside the classes?


Thanks for any help :)


Andrew
 
V

Victor Bazarov

Andrew said:
I have a super class, and several sub classes which all extend the super.

I want to add a static variable to the super class so that it exists in
all its sub classes, how do i do this?

Just do it(TM)
All my attempts have resulted in symbol referencing errors.

Did you actually define it or only declare it?
Do i declare the variable in the super? If so, how, and then how do i
access it from the sub classes, and also from outside the classes?

class super {
public:
static int variable;
};

int super::variable = 42;

class sub : public super {
int foo() {
return variable;
}
};

int main() {
return super::variable;
}

What's the problem?

V
 

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
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top