#include <iostream> int main() { int i = 1024; // 别名 int &ref = i; std::cout << &i << std::endl; std::cout << i << std::endl; std::cout << &ref << std::endl; std::cout << ref << std::endl; return 0; }
输出了同样的内存地址和值
0x7ffd9976dadc
1024
0x7ffd9976dadc
1024
#include <iostream> int main() { int i = 1024; int ref = i; std::cout << &i << std::endl; std::cout << i << std::endl; std::cout << &ref << std::endl; std::cout << ref << std::endl; return 0; }
内存地址后移动了四个单位
说明多申请了一块内存,并进行了值拷贝操作
0x7ffd6163efa0
1024
0x7ffd6163efa4
1024