关于c/c++中的static

来源:互联网 发布:windows系统维护工具 编辑:程序博客网 时间:2024/06/07 13:10
#include "iostream"using namespace std;/*-------Begin----------C语言部分-------Begin----------*///c语言中的static两种用法://1、用于变量,知道程序结束才释放//2、用于函数,限制在本.c文件中不能被外界调用//状态保留//用static修饰变量void GetStatic(){    int a = 10;    //b只初始化一次    static int b = 10;    printf("a:%d b:%d \n", a, b);    a++;    b++;}//这个函数只能在这个c文件中被使用static void myprintf(){    printf("锄禾日当午,汗滴禾下土");}/*-------End----------C语言部分-------End----------*//*-------Begin----------C++部分-------Begin----------*///指针做函数参数class Test{private:    int m_a;     //属于具体的对象    int m_b;    static int count;    //static修饰的变量或者函数,属于这个类,不属于具体的对象public:    Test(int a, int b)    {        m_a = a;        m_b = b;        count ++;        cout<<"构造执行"<<endl;    }    ~Test()    {        cout<<"析构执行"<<endl;    }    void  setCount(int i){count=i;}    static int getCount()    {        return count;    }};//初始化方法int Test::count  = 0;void main(){    Test t1(1, 3), t2(3, 4);    //更改static变量方法    t1.setCount(200);    //两种调用方法    cout<<Test::getCount()<<endl;;    cout<<t2.getCount()<<endl;;    system("pause");}
0 0
原创粉丝点击