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

512 Views Asked by At

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)

1

There are 1 best solutions below

0
On BEST ANSWER

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