mutable关键字

需要修改常函数里的数值,需要加上mutable关键字

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Person
{
public:
    Person()
    {
        //构造中修改属性
        //this永远指向本体
        //相当于 Person * const this  不允许修改指针的指向
        this->m_A = 0;
        this->m_B = 0;
    }

    void showInfo() const   //const加在函数的括号后面表示常函数 不允许修改指针指向的值 因为void showInfo()相当于void showInfo(Person * const this)
    {                        //所以在函数后再加const 相当于const Person* const this 既不能修改指针的指向 也不能修改指针指向的值
        this->m_A = 1000;    //error
        this->m_B = 1000;    //可以修改
    }

    int m_A;
    mutable int m_B;    //就算是常函数,也想要修改 需要加mutable关键字
};

int main()
{
    system("Pause");        //阻塞功能
    return EXIT_SUCCESS;    // 返回正常退出
}
原文地址:https://www.cnblogs.com/yifengs/p/15170741.html