Ensuring a Single Copy of a Member Variable

Problem

You have a member variable that you want only one instance of, no matter how many instances of the class are created. This kind of member variable is generally called a static member or a class variable, as opposed to an instance variable, which is one that is instantiated with every object of a class.

Solution

Declare the member variable with the static keyword, then initialize it in a separate source file (not the header file where you declared it) as in Example 8-5.

Example 8-5. Using a static member variable

// Static.h
class OneStatic {
public:
 int getCount( ) {return count;}
 OneStatic( );
protected:
 static int count;
};

// Static.cpp
#include "Static.h"

int OneStatic::count = 0;

OneStatic::OneStatic( ) {
 count++;
}

// StaticMain.cpp
#include 
#include "static.h"

using namespace std;

int main( ) {
 OneStatic a;
 OneStatic b;
 OneStatic c;

 cout << a.getCount( ) << endl;
 cout << b.getCount( ) << endl;
 cout << c.getCount( ) << endl;
}

 

Discussion

static is C++'s way of allowing only one copy of something. If you declare a member variable static, only one of it will ever be constructed, regardless of the number of objects of that class that are instantiated. Similarly, if you declare a variable static in a function, it is constructed at most once and retains its value from one function call to another. With member variables, you have to do a little extra work to make sure member variables are allocated properly, though. This is why there are three files in Example 8-5.

First, you have to use the static keyword when you declare the variable. This is easy enough: add this keyword in the class header in the header file Static.h:

protected:
 static int count;

Once you have done that, you have to define the variable in a source file somewhere. This is what allocates storage for it. Do this by fully qualifying the name of the variable and assigning it a value, like this:

int OneStatic::count = 0;

In Example 8-5, I put this definition in the file Static.cpp. This is what you have to do; you should not put the definition in the header file. If you do, storage will be allocated in each implementation file that includes the header file, and either you will get a linker error or, worse, there will be several instances of this member variable in memory. This is not what you want if you need a static member variable.

In the main file, StaticMain.cpp, you can see what happens. Several instances of the class OneStatic are created, and the default constructor of OneStatic increments the static member variable by one each time. As a result, the output from main in StaticMain.cpp is:

3
3
3

Each call to getCount returns the same integer value, even though each is invoked on a different instance of the class.

Building C++ Applications

Code Organization

Numbers

Strings and Text

Dates and Times

Managing Data with Containers

Algorithms

Classes

Exceptions and Safety

Streams and Files

Science and Mathematics

Multithreading

Internationalization

XML

Miscellaneous

Index



C++ Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2006
Pages: 241

Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net