If I have the following:
class A {
int foo() const {
j++;
return i;
}
int& foo() {
return i;
}
int i;
mutable int j;
};
then obviously something like
A a;
a.foo() = 5;
calls the non-const version. But what conditions need to be met to make sure that a call is to the const or non-const version for a few examples...
int i = a.foo(); //I would expect to call the const. Is it guaranteed?
const int j = a.foo(); //Ditto
const int& l = a.foo(); //Ditto
int& k = a.foo(); //I would expect the non-const
foobar(k); //foobar is "void foobar(int)"
return; //but the compiler could potentially decide the const version is fine.