In C++, a
static data member works as a pseudo global variable between all instances of a
class. In other words, it creates a variable that is the same for all members of a
static int experiment;
test test::experiment; //Must be initialized outside class to exist
bob.experiment = 5;
cout << bob.experiment << endl << george.experiment;
The code results in the following output:
As you can clearly see, the change made in the "bob" instance of the test class was also reflected in the "george" instance of the test class.
Also, if the static variable is public, then you can do this:
test::experiment = 5;
This lets you change the static variable without referencing any specific instance of the class. Possible applications for static data members include a memory pool for overloading the
delete operators, or saving some memory by using this pseudo global variable instead of a plain 'ol global variable.