"ek" <ek*****@yahoo.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
This first example does not work (cannot be overloaded):
int& operator()(int a) { // (1)
return a;
}
int const& operator()(int a) { // (2)
return a;
}
Indeed, you can't overload based solely on return type. Besides, the both
yield undefined behaviour because you're returning a reference to a local
variable, but I know that's not your point ;)
but if I change (2) to (3):
int operator()(int a) const { // (3)
return a;
}
it works (overloading works)
Because (3) only works on a const *this, whereas (1) also works on a
non-const *this.
struct A
{
void func();
void func() const;
}
int main()
{
A a;
const A ca;
a.func(); // calls void A::func()
ca.func(); // calls void A::func() const
}
- Sylvester