类的静态类型处理, 可以避免全局变量使用....... 请看如下代码:
- #include "stdafx.h"
- #include
- using namespace std;
- class STATIC_A{
- public:
- STATIC_A (); // 构造函数
- ~STATIC_A (){
- }; // 析构函数
- void plus(){
- c=c+100;
- };
- void print();
- public:
- int a;
- float b;
- static int c;
- };
- int STATIC_A::c=100;
- void STATIC_A::print(){
- cout<<"a="<<a<<"\n"<<"b="<<b<<"\n"<<"c="<<c<<endl;
- };
- STATIC_A::STATIC_A (){
- a=1;
- b=10*a;
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- const int t=6;
- STATIC_A A[t];
- for (int i=0;i
- A[i].plus();
- A[i].print();
- }
- while (1);
- return 0;
- }
来源: http://www.bubuko.com/infodetail-3241076.html