Home Segments Index Top Previous Next

652: Mainline

Another way to prevent memory leaks is to keep track of the number of objects that you have created by having constructors and destructors increment and decrement an object counter. You could arrange for a global variable to be the object counter, but you will be a better programmer if you use what is called a static member variable.

Normal member variables are duplicated once for each class object. Static member variables are not—there is just one static member variable for the entire class in which the static member variable is defined. Accordingly, a static member variable is a convenient place to store information that pertains to a class as a whole.