Static Member Variables
Rember class attributes?
OO: class attributes <-> C++: static member variables
OO: object instance attributes <-> C++: non-static member variables.
Every object instance has its own set of object instance attributes
All object instances share the same class attributes.
In C++ we use the "static" keyword.
Example:
class MakesNoSense {
private:
static int counter;
int someVar;
public:
void doSomething();
int getCounter();
}
...
void MakesNoSense::doSomething(int a)
{
counter = a;
someVar = a;
}
...
MakesNoSense *a = new MakesNoSense(), *b = new MakesNoSense();
a->doSomething(1);
b->doSomething(2);
cout << a->getCounter(); // Discussion: What will this print?
delete a; delete b;