T& f() { // some code ... } const T& f() const { // some code ... }
I've seen this a couple of times now (in the introductory book I've been studying thus far). I know that the first const makes the return value const, in other words: unmodifiable. The second const allows that the function can be called for const declared variables as well, I believe.
But why would you have both functions in one and the same class definition? And how does the compiler distinguish between these? I believe that the second f() (with const) can be called for non-const variables as well.
There are legitimate uses of having two member functions with the same name with one const and the other not, such as the begin and end iterator functions, which return non-const iterators on non-const objects, and const iterators on const objects, but if it's casting from const to do something, it smells like fish.
The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.
const member functions may be invoked for const and non-const objects. non-const member functions can only be invoked for non-const objects. If a non-const member function is invoked on a const object, it is a compiler error.
C++ allows member methods to be overloaded on the basis of const type. Overloading on the basis of const type can be useful when a function return reference or pointer. We can make one function const, that returns a const reference or const pointer, other non-const function, that returns non-const reference or pointer.
But why would you have both functions in one and the same class definition?
Having both allows you to:
const
object, and only look at the result.With only the first, you couldn't call it on a const
object. With only the second, you couldn't use it to modify the object it returns a reference to.
And how does the compiler distinguish between these?
It chooses the const
overload when the function is called on a const
object (or via a reference or pointer to const
). It chooses the other overload otherwise.
I believe that the second f() (with const) can be called for non-const variables as well.
If that were the only overload, then it could. With both overloads, the non-const
overload would be selected instead.
But why would you have both functions in one and the same class definition?
Sometimes you want to provide different semantics for same operation depending on whether it's being invoked on const
object OR non-const
object. Let's take an example of std::string
class:-
char& operator[](int index); const char& operator[](int index) const;
In this case when operator[]
invoked through const
object you won't let user to change the content of the string.
const std::string str("Hello"); str[1] = 'A'; // You don't want this for const.
On the other hand,In case of non-const string you let user to change the content of string. That's why a different overload.
And how does the compiler distinguish between these?
Compiler checks whether that method is invoked on const
object OR non-const
object and then appropriately call that method.
const std::string str("Hello"); cout << str[1]; // Invokes `const` version. std::string str("Hello"); cout << str[1]; // Invokes non-const version.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With