C/C++

C与引用和变量复制

2017-09-20  本文已影响2人  一木之夏

int& r = i; 和 int r = i; 不同之处应该是内存的分配吧,后者会再开辟一个内存空间

#includeusing namespace std;

int main ()

{

int i;

int& r = i;

i = 5;

cout << "Value of i : " << i << endl;

cout << "Value of i reference : " << r  << endl;

cout << "Addr of i: " << &i << endl;

cout << "Addr of r: " << &r << endl;

int x;

int y = x;

x = 6;

cout << "Value of x : " << x << endl;

cout << "Value of y : " << y  << endl;

cout << "Addr of x: " << &x << endl;

cout << "Addr of y: " << &y << endl;

return 0;

}

输出结果:

Value of i : 5

Value of i reference : 5

Addr of i: 0x7ffffc9517b4

Addr of r: 0x7ffffc9517b4

Value of x : 6

Value of y : 4197104

Addr of x: 0x7ffffc9517b0

Addr of y: 0x7ffffc9517ac

上一篇下一篇

猜你喜欢

热点阅读