Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same function with const and without - When and why?

Tags:

c++

constants

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.

like image 242
Beko Avatar asked Jan 07 '15 17:01

Beko


People also ask

How is it possible to have both const and non-const version of a function?

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.

Is const the same as function?

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.

Can a const function call a non-const function?

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.

Can const function be overloaded?

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.


2 Answers

But why would you have both functions in one and the same class definition?

Having both allows you to:

  • call the function on a mutable object, and modify the result if you like; and
  • call the function on a 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.

like image 118
Mike Seymour Avatar answered Oct 08 '22 19:10

Mike Seymour


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. 
like image 25
ravi Avatar answered Oct 08 '22 18:10

ravi