0

I am learning C++. In the course I am doing, we have now covered the section about classes.

I now know that functions can be overloaded; given different types of parameters, a different implementation can be made. However, in the case of member functions, the caller sort of also becomes part of the function. Is there any way we can use function overloading to pick an appropriate implementation depending on if the caller object is const or not? Or if it is an lvalue vs an rvalue?

So if there is a class Foo, is there a way to have different implementations of myFoo.bar() depending on if myFoo is const, or if you pass it an rvalue such as in the case of Foo{}.bar()?

Qqwy
  • 5,214
  • 5
  • 42
  • 83
  • You can do overloading based on constants i.e. you can have two function out of which one is constant and other is not. – SACHIN GOYAL Sep 30 '16 at 07:26
  • Starting from C++11 you have ref-qualifiers for overloading based on the value category of an implicit object parameter. – Piotr Skotnicki Sep 30 '16 at 07:27
  • @bo-persson I believe that this question is more general than the question that you believe this to be a duplicate of. – Qqwy Sep 30 '16 at 07:32

0 Answers0