Intermediate Business Programming with C++

Above static data members of a class were introduced. However, not only does C++ have static data members but it also has static member functions as well. The word static must be placed on the declaration of the function only. These functions may be placed into any access section but of course they must obey the access rules. These functions can be called by the class name and the scope resolution operator or the dot operator with an object (although the later is meaningless since static member functions do not have access to non static member data).

A static function does not have a this pointer when it is called. As a result, they can not call a non static data member of an object directly. The only way it can access a non static data member is indirectly by passing the class object to the function as an argument. Therefore, it does not make sense to use the dot operator with a static member function because static member functions will only access static data members which are not data members of a particular class object.

The main purpose of a static member function is to initialize global properties of a class which should be done to the class or all objects. There are some properties which should be initialized only once and therefore could not be included in a constructor which would be called each time a new object was defined. Using static member functions therefore permits the isolation of low level activity inside of the class as is done with windows.h

Static member functions:

For an example of class static member functions see stacfnct.cpp.

Категории