#include <iostream> using namespace std; void foo(int const&) { cout << "const reference" << endl; } void foo(int const*) { cout << "const pointer" << endl; } int main() { int *hi; foo (hi); //const pointer foo((*hi)); //const reference foo(&(*hi));//const pointer }
The deal here is links, and the pointers are different. A pointer is a unique type where, as a reference to a value, it is no different from the value itself or, rather, an alias of the object. So, for example, this version of the above code will not compile.
#include <iostream> using namespace std; void foo(int const&) { cout << "const reference" << endl; } void foo(int) { cout << "hi there" << endl; } int main() { int hi; foo(hi); //const reference }
Since the declarations of foo are mixed. The compiler cannot decide between them.
Chriscm
source share