Software & Finance





C++ - Mutable Will Never be Constant





Mutuable is a keyword used in a class for a member variable and that variable will never be constant and can changed even with in a const function.

 

class MyClass

{

private:

    mutable int m;

    int a;

    int b;

public:

    void func1() const

    {

        // a = 10; Error: l-value specifies const object

        // b = 20; Error: l-value specifies const object

        m = 30;

    }

    int getA() const

    {

        return a;

    }

};