Accessing class variable from another class

I'm looking for how to create a variable in each of two classes, with these properties:

1) Variables have the same name.
2) Each variable is per-class (i.e. not per-instance).
3) Each variable is accessible from outside the class, presumably by somehow qualifying the variable name with a class name (or a namespace?).
4) The variables obviously must not collide with each other in global space.

It's such a simple need, yet I have read numerous postings, and haven't run across a suitable solution. I'm mostly interested in the 'best practices' way of doing this, but am also interested in anything that works.

The following is pseudo code, not valid c++ code, but illustrates what I want to do:

class ClassA {
public static int counter;
}
class ClassB {
public static int counter;
}
class ClassC {
int sub() {
int a = ClassA::counter;
int b = ClassB::counter;
}
}

Thanks
You essentially have it. Just use the valid syntax:
http://en.cppreference.com/w/cpp/language/static
Topic archived. No new replies allowed.