ICS 45C Spring 2022
Notes and Examples: Static Members

Includes a code example with the moniker StaticMembers


Members of a class, revisited

As we've seen before, classes in C++ contain a collection of members, each of which specifies some property (e.g., a variable, a function) that belongs to each individual object of that class. Every object of a class contains a separate copy of every member variable, while member functions are called on particular objects of the class. For example, consider the Person class we've seen previously.

class Person
{
public:
    Person(const std::string& name, unsigned int age);

    std::string getName() const;
    unsigned int getAge() const;

private:
    std::string name;
    unsigned int age;
};

Person is a blueprint for a new kind of object, and we can create as many objects from that blueprint as we'd like. Each Person object will have a name and an age, separate from the others; similarly, when we want to ask a Person "What's your name?" or "What's your age?", we'll have to ask a particular Person, since the answer will likely be different from one Person to another. Much more often than not, when you want to add a member variable or a member function to a class, you'll want it to be this way, i.e., a property of objects of your class, as opposed to a property of the entire class as a whole.

However, sometimes you'll want a different design ability: the ability to make something into a property of the entire class, as opposed to individual objects of that class. You might, for example, want a member variable that's shared amongst all of the objects of your class — or, thought differently, one that belongs to the class instead of belonging to each object. You might want a member function that you call on the class as a whole, such that you won't even need an object of that class to call it, but that nonetheless has something to do with the class and belongs as part of it. In times like these, what you need are static members.


Static members

The static members of a class are the ones that belong to the entire class, instead of belonging to individual objects of that class. Static member variables exist whether you have objects of the class or not, independent of the individual objects of that class. Static member functions are called on the class, rather than being called on an object of the class; you don't even need an object of the class to call them, and they don't have access to non-static member variables unless you qualify them with an object of the class.

At first blush, this might seem like a strange language feature to want, but there are actually a good number of uses for it. The trick is to realize that you want to implement a capability that is class-wide, i.e., that has to do with the entire class, as opposed to individual objects of that class.

An example of that would be implementing a feature to keep track of how many objects of some class exist. The code example below explores that idea by implementing a class called Widget. Widgets aren't especially interesting on their own — they store an integer ID and nothing else — but what is interesting about them is that it's possible, at any given time, to find out how many Widget objects exist (i.e., have been allocated but not yet deallocated). The trick is to add two things to the Widget class:


The code

The official moniker for this code example is StaticMembers, so your best bet is to do this:

Alternatively, you can click the link to the tarball below: