Can anyone tell me how you can make sure a method can only access and not change private data members in a C++ class instance is the method does not have any arguments. I am writing a program that draws shapes, and I tried this
void drawCircle() const;
but I have been unable to determine whether the data members remain unchanged. Thanks
Declaring a member-function
const(like your example) means that it won’t change any member variables via (the implicit)thispointer. The compiler enforces this.So the following won’t compile:
Of course, it doesn’t prevent you playing tricks like casting away the const-ness.