「const」関数内のメンバー変数の値を変更することは可能ですか? 質問する

「const」関数内のメンバー変数の値を変更することは可能ですか? 質問する

定数変数の値はポインタートリックを通じて変更できますが、次のようなことは可能ですか?

    class A (){
       int x;
    public:
       void func () const {
          //change value of x here
    }
}

ベストアンサー1

宣言するx mutable

class A (){
   mutable int x;
public:
   void func () const {
      //change value of x here
   }
}; 

おすすめ記事