|
![]() |
|||
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
Steps
How It Works Lets begin by examining the declaration of Base in STATIC.CPP. The first declaration in the class is the default constructor, as shown here: Base( ) { ++instanceCnt ; } The constructor is defined as an inline member function because its definition occurs inside the classs declaration. The constructor contains only a single statement, incrementing the static member variable instanceCnt. The next declaration, which is also an inline member function, is the destructor. The logic found in the destructor is the inverse of the constructor: to decrement the static data member instanceCnt. The third declaration int count( ) const { return( instanceCnt ) ; } is also the definition of a const member function that returns the value contained in instanceCnt. This member function is also considered for inlining because its definition occurs inside the class declaration. Rounding out the class declaration, in the private section, you find the declaration of a static member, as shown here: static int instanceCnt ; This declaration is no different from any other data member declaration, except for the static keyword prefix. What is the difference between a static and non-static data member? Only a single instance will exist for a static data member at runtime, no matter how many objects are created for the class. For non-static data members, a copy will exist for every object instance. Data members that are non-static are also referred to as instance variables. This suggests that each instance of a class will get its own unique set of non-static data members. The opposite occurs with static data membersno matter if there is one class object or thirty objects at runtime, a single copy of each static data member will exist for all objects instantiated. All instances of a class share any static data members declared. The next line of code in the file int Base::instanceCnt = 0 ; defines and initializes the static data member. All static data members of a class must be defined outside of the class declaration. Notice that the name of the class followed by the scope resolution operator is given, in addition to the name of the static data member. The first statement within the main function declares an array of 10 pointers of type Base. The second statement within main defines i as an integer variable. Next, a for loop is introduced. Ten objects of type Base are created in the free store. A pointer to each of those objects is accumulated in the array named b. The next line of code cout << Instance count #1: << b[ 0 ]->count( ) << endl ; prints out the contents of the static data member. Moving through the code, you find a second for loop. This loop is used to deallocate the objects that were allocated in the previous loop. The difference is that the loop only counts up to ten because you want to leave one object around for the next program statement: cout << Instance count #2: << b[ 0 ]->count( ) << endl ; The value 1 is printed, as expected. The line that follows this cout statement deletes the last remaining object. Comments Class member functions can also be declared static. A static member function can be invoked, even if there are no instances of the class where it is declared. The only data members that a static member function can access are static data members because a static member function is not associated with any particular instance of a class. The following class declaration contains a declaration and the definition of a static member function: class Base { public: static int count(void) ; } ; int Base::count( ) { //... } Notice that the static keyword is not used at the definition of the member function. Because count is declared with public visibility, it can be invoked as shown in the following code snippet: int main() { Base::count() ; //... } Because it belongs to a class, a static data member does not pollute the global namespace. This helps to reduce name clashes that might otherwise occur. You should not define a static data member inside the header file containing the class declaration; define it in the implementation file.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|