`

c++ 静态变量使用

    博客分类:
  • c++
c++ 
阅读更多
class T{
public :
     T();
     void sysi();
     void addi();
     int k;
};

#include<iostream>
#include "t.h"
using namespace std;
//static 加与不加效果是一样的,私有的静态变量
int i = 0;
T::T(){

k=0;// 局部变量
}
void T::sysi(){

cout<<"k is:"<<k<<endl;
cout<<"i is:"<<i<<endl;
}


void T::addi(){

k = k+1;
i = i+1;

}

#include<iostream>
#include "t.h"
using namespace std;

int main()
{

T *t = new T();
T *t1 = new T();
t1->addi();
t->sysi();

t1->addi();
t->sysi();
/*
error!
i is not i t.h
int a = T::i;

cout <<"a is :"<< a<< endl;
*/
delete t;
delete t1;

}

result:
k is:0
i is:1
k is:0
i is:2
aircoder@aircoder:~/c++/static_t$
------------------------------------------------------
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics