我正在学习 C++ 的参考,并尝试了 Thinking in C++ 中的以下代码:
但是,我发现如果我没有将引用转换为 'long' 类型,则f和g的引用是相同的,我认为这没有意义,并且它们的值都是1而不是显示的数字十六进制,谁能解释一下?
谢谢。
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;
} //
结果
f() normal: 1
f() long: 4375104512
g() normal: 1
g() long: 4375104608
j normal: 0x7fff6486b9c0
j long: 140734879939008
k: 140734879939004
k: 140734879939004