// Example of the static keyword static int i; // Variable accessible only from this file static void func(); // Function accessible only from this file int max_so_far( int curr ) { static int biggest; // Variable whose value is retained // between each function call if( curr > biggest ) biggest = curr; return biggest; } // C++ only class SavingsAccount { public: static void setInterest( float newValue ) // Member function { currentRate = newValue; } // that accesses // only static // members private: char name[30]; float total; static float currentRate; // One copy of this member is // shared among all instances // of SavingsAccount }; // Static data members must be initialized at file scope, even // if private. float SavingsAccount::currentRate = 0.00154;
本文地址:http://com.8s8s.com/it/it23824.htm