I have two overloaded functions, one "call by value" and the other "call by reference".
int f (int a)
{
//code
}
int f (int &a)
{
//code
}
But if I pass const int
, it calls the "pass by value" function, why?
const int a=3;
f(a);//calls the call by value function.Why?
Because a
is a const int
, and so that tells the compiler that you do not wish to modify a
. a
can't be passed by reference (only by const&
), because if it is a reference, f
could modify it, but f
is not allowed to because a
is const
.
So the only overload that is legal is the pass by value one - int f(int a)
.