-4

In my code myclass has two two const functions which are being called by some other const functions. In this class I am not modifying anything except bool variable.

In this code bool variable "isCreated" is being used to check if something is created or not. If not created then create it in function fooUpdate().

myclass.h

class myclass
{
  public:
     myclass()
     {}

     ~myclass() {}

     void fooUpdate() const;
     void fooCreate(bool arg) const;

     bool isCreated;  //should I make it mutable or static
};

myclass.cpp:

void myclass::fooCreate(bool arg) const  //In this function I am not modifying anything except bool variable. 
{
    isCreated = arg;
    //create
}

void myclass::fooUpdate() const  
{
    if(!isCreated) 
    {
        //create
    }
    else
    {
       //update
    }

}

some other classes

//These functions will always be const. Not in my scope to change it.

create() const {
    lClass.fooCreate(true);
}

update() const {
    lClass.fooUpdate();
}

Now I have two options either make isCreated mutable or static. I just want to know which is better option ?

2 Answers2

3

You are misusing const qualifier for member functions. Both fooUpdate and fooCreate should not be marked as const because they are supposed to modify internal state of an object. Note that "they are called by some const function" is not a valid reason to mark function as const, it actually indicates a potential logic flaw in your code.

If the calling code can not be changed then you can utilize a PImpl idiom and utilize wrapper class for legacy code:

class myclass
{
  private:
    bool isCreated;

  public:
    myclass() {}

    ~myclass() {}

    void fooUpdate()
    {
      if(!isCreated) 
      {
        isCreated = true;
        //create
      }
      else
      {
        //update
      }
    }

    void fooCreate(bool arg)
    {
      isCreated = arg;
    }
};

class myclasswrap
{
  private:
    const ::std::unique_ptr<myclass> m_p_impl;

  public:
    myclasswrap(): m_p_impl(make_unique<myclass>()) {}

    ~myclasswrap() {}

    // can call non-const-qualified methods of myclass just fine
    void fooUpdate() const {m_p_impl->fooUpdate();}

    void fooCreate(bool arg) const {m_p_impl->fooCreate();}
};
user7860670
  • 35,849
  • 4
  • 58
  • 84
  • This is legacy code which I can't change. but I have to check the bool variable value. – user3086141 Nov 11 '17 at 14:50
  • @user3086141 Maybe you can mark `lClass` as `mutable`? Or utilize a `PImpl` idiom so your class wrapper for this legacy code stores a pointer to actual implementation. This way it will be possible to call even not-const-qualified methods of implementation class from const-qualified methods of wrapper class without utilizing `mutable` or `static` – user7860670 Nov 11 '17 at 14:57
  • Thank you very much your help but I can't create wrapper because I am calling one API where I am getting this class const Pointer. In this class I am not modifying anything except bool variable. I think mutable is best approach. – user3086141 Nov 11 '17 at 16:38
2

They're completely different keywords serving completely different purposes:

  • static means that the variable will be shared by all instances of the class. It's basically a global variable.

  • mutable allows fine-grained mutation of member variables even in const-qualified member functions.

They're really not comparable.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416