1 #include <iostream>
2 using namespace std;
3 class Node{
4 int x;
5 public:
6 Node(const Node & n){//去掉copy函数关于h的输出结果神奇般相同,原因未知
7 x = n.x;
8 }
9 Node(int x=0):x(x){
10
11 }
12 };
13 int temp ;
14 int & F(){
15 temp*=2;
16 return temp;
17 }
18 int F1(){
19 temp*=2;
20 return temp;
21 }
22 Node F2(){
23 Node u(5);
24 cout << &u << " ";
25 return u;
26 }
27 int main(){
28 temp = 5;
29 int &d = F(); //引用给引用,他们地址相同
30 int e = F(); //引用给非引用,发生了复制
31 const int & f = F();//把引用值赋值给常引用,他们的地址相同
32 const int & g = F1();//对于内置数据类型,把非引用值赋值给常引用,他们的地址不同
33 cout << &d << " " << &temp << endl;
34 cout << &e << " " << &temp << endl;
35 cout << &f << " " << &temp << endl;
36 cout << &g << " " << &temp << endl;
37 const Node & h = F2();//对于类对象类型,把非引用值赋值给常引用,他们的地址可能不同
38 cout << &h << endl;
39 cout << d << " " << temp << endl;
40 return 0;
41 }