Why use a const member function?

When you declare a member function as const, like in

int GetValue() const;

then you tell the compiler that it will not modify anything in the object.

That also means you can call the member function on constant object. If you don’t have the const modifier then you can’t call it on an object that has been defined as const. You can still call const member functions on non-constant objects.

Also note that the const modifier is part of the member function signature, which means you can overload it with a non-const function. That is you can have

int GetValue() const;
int GetValue();

in the same class.

Leave a Comment