When you declare a member function const , the this pointer becomes const inside the const function when it is called on the object.
The value of the const member function prevents direct or immediate modification of the data members of the class.
Direct implies what you do in your program (changing the data members directly in the const member function, which violates its purpose). It is perfectly normal to perform any operations with data members if you do not modify them. In addition, you can call other const member functions inside the const member function.
And indirect means that you cannot even call other member functions of a non-const class, because they can modify data elements.
Usually const member functions are used when you just want to get / read values. Therefore, in your case, you should not use the const member function.
Alternatively, you can call non-const and const member functions on a non-const object.
source share