C++中的静态类元素

在C/C++关键词“static”可以用来定义一个函数中的变量为静态变量,这样导致函数销亡时被static修饰过的变量值并不会随之销亡,当第二次调用时该变量值仍会在原来的基础上再做修改,例如以下程序:

#include <iostream>

using namespace std;


int TestFunctionA(){
    int a = 0;
    a++;
    return a;
}

int TestFunctionB(){
    static int b = 0; //在函数B中将该函数中的全局变量定义为静态变量
    b++;
    return b;
}

int main(){
    int TestFunctionA();
    int TestFunctionB();
    printf("call functionA 10 times:\n");
    for(int i = 0; i < 10; i++){
        cout << TestFunctionA() << endl;
    }
    printf("call functionB 10 times:\n");
    for(int i = 0; i < 10; i++){
        cout << TestFunctionB() << endl;
    }
}

运行结果如下:
在这里插入图片描述
显然,此处调用函数A 10次,每次运行的结果均为1,因为当调用结束后,TestFunctionA会自动凋亡,同样地,其中的变量a也随之凋亡,第二次重新调用时,变量被重新定义,导致每次调用返回的结果均相同。但是TestFunctionB中对变量b采用static修饰之后,虽然函数TestFunctionB凋亡了,但是其中的静态变量仍然存在,当第二次再被调用时值依然保持上次调用之后的值。这样,在大型项目中可以很好的记录,某一模块或者某个函数被调用过多少次。
同样地,在C++的类中也可以用static来修饰某一变量或者某一方法。在类中用static修饰过的属性将不再受到对象的凋亡而改变值。当同一个类被多次调用时,第二次被调用时静态属性会随前一次的值而改变。
上一段代码来解释这个内容:

#include <iostream>

using namespace std;

class TestA{
public:
    int a = 0;

    TestA(){
        cout << "initialization class TestA\n";
        a++;
    }

    ~TestA(){
        cout << "Destructor class TestA\n";
    }
};

class TestB{

public:
    static int b;

    TestB(){
        cout << "initialization class TestB\n";
        b++;
    }

    ~TestB(){
        cout << "Destructor class TestB\n";
    }
};

int TestB::b = 0; //对类B中的静态属性赋值,独立于主函数和class之外

int main(){
    printf("initialize class TestA 5 times:\n");
    for(int i = 0; i < 5; i++){
        TestA* testa = new TestA();
        cout << testa->a << endl;
        delete testa;
    }

    printf("\n\ninitialize class TestB 5 times:\n");
    for(int i = 0; i < 5; i++){
        TestB* testb = new TestB();
        cout << testb->b << endl;
        delete testb;
    }
}

运行结果:
在这里插入图片描述
此处由于类B中的属性b是被static修饰的,因此其需要在独立于主函数和类之间对其进行初始化赋值,由运行结果可见,虽然B被实例化的对象凋亡了,但其属性值依然不变。这也可以用于记录一个类被实例化过多少次。
在类中,静态方法不能修改或调用非静态属性或方法。同时静态属性或在静态方法内不能使用this指向该属性或任何属性。例:

#include <iostream>

using namespace std;

class A{
    static int attribute1;
    int attribute2;
public:
    A(){
        this->attribute2 = 0;
    }
    static void increment1(){
        cout << "static method change static attribute" <<endl;
        //this->attribute1++;  非法, attribute1为静态属性不能使用this修饰
        attribute1++; //合法
    }

    void increment2(){
        cout << "non-static method change non-static attribute" <<endl;
        this->attribute2++;   //合法
    }
    static void increment3(){
        //this->attribute2++;  //非法, 形态方法内不能用this指向某个属性或方法
        //attribute2++;  //非法, 静态方法内不得调用或修改非静态属性或方法
        cout << "static method change non-static attribute" <<endl;
    }
    void increment4(){
        this->attribute1++;  //非法, 静态属性不能用this指向
        cout << "non-static method change static attribute" <<endl;
    }
};

int A::attribute1 = 0;

int main(){
    A* a = new A();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值