C++:mutable成员在非成员函数中依旧mutable

struct foo
{
    int start{0};
    mutable int end{0};

    void bar() const
    {
        start = 1;  // 错误:由于正在通过常量对象访问“start”,因此无法对其进行修改
        end = 1;    // ok
    }
};

void global_bar(const foo &obj)
{
    obj.start = 2;  // 错误:由于正在通过常量对象访问“start”,因此无法对其进行修改
    obj.end = 2;    // ok
}

陕ICP备2025078817号-1 陕公网安备61011202001108号