c++constantssetterpass-by-const-reference

Can we have a setter member function as const referenced from a const object?


I have a class like this :

class XYZ {
public :
  bool var1 = true;
  ABC *var2 = nullptr;

public :
  XYZ() = default;

  void SetVar1(bool flag)
  {   var1 = flag;  }

  void SetVar2(ABC *abc)
  {   var2 = abc;  }
}

I want to know that if my objects for XYZ are referenced as const everywhere in my codebase, can I use these setters? P.S. I have tried with const Setters and it doesn't work (I knew it doesn't make much sense but I was stuck in the compilation error so tried it anyway)


Solution

  • Once u make ur object const u can't change value of any varables in this object