I'm learning about the reference of C++, and I tried the following code from Thinking in C++:
However, I found that if I didn't cast the reference to 'long' type, the reference for f and g is the same, which I think doesn't make sense, and their value are both 1 instead of a number displayed in hexadecimal, could anybody explain that?
Thanks.
include <iostream>
using namespace std;
int dog, cat, bird, fish;
void f(int pet) {
cout << "pet id number:" << pet << endl;
}
void g(int pet) {
cout << "pet id number:" << pet << endl;
}
int main() {
int i,j, k;
cout << "f() normal: " << &f << endl;
cout << "f() long: " << (long)&f << endl;
cout << "g() normal: " << &g << endl;
cout << "g() long: " << (long)&g << endl;
cout << "j normal: " << &j << endl;
cout << "j long: " << (long)&j << endl;
cout << "k: " << (long)&k << endl;
k=2;
cout << "k: " << (long)&k << endl;
} //
Result
f() normal: 1
f() long: 4375104512
g() normal: 1
g() long: 4375104608
j normal: 0x7fff6486b9c0
j long: 140734879939008
k: 140734879939004
k: 140734879939004