Static Member Functions


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:

  • do not have the pointer this.

  • can not access non static members of the class only static members.

  • can not be a constant or a volatile function.

  • should not be called with a dot operator but instead they should be called by the class and the scope resolution operator.

  • have the keyword static used on the function's declaration.

  • are designed to manipulate the static attributes of the class.

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




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

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