c++ 别名

扫码查看
#include <iostream>
int main()
{
int i = ;
// 别名
int &ref = i;
std::cout << &i << std::endl;
std::cout << i << std::endl;
std::cout << &ref << std::endl;
std::cout << ref << std::endl;
return ;
}

输出了同样的内存地址和值

0x7ffd9976dadc
1024
0x7ffd9976dadc
1024

#include <iostream>
int main()
{
int i = ;
int ref = i;
std::cout << &i << std::endl;
std::cout << i << std::endl;
std::cout << &ref << std::endl;
std::cout << ref << std::endl;
return ;
}

内存地址后移动了四个单位

说明多申请了一块内存,并进行了值拷贝操作

0x7ffd6163efa0
1024
0x7ffd6163efa4
1024

04-14 03:25
查看更多