c++中的赋值问题

来源:百度知道 编辑:UC知道 时间:2024/06/24 05:42:58
#include <iostream>
int main()
{
int actualint=123;
int otherint =actualint;

std::cout<<actualint<<std::endl;
std::cout<<otherint<<std::endl;

otherint++;
std::cout<<actualint<<std::endl;
std::cout<<otherint<<std::endl;

actualint++;
std::cout<<actualint<<std::endl;
std::cout<<otherint<<std::endl;

return 0;

}

和下面这两个程序段的结果是不一样的,哪位那能帮着解释一下,谢谢了
#include <iostream>
int main()
{
int actualint=123;
int& otherint =actualint; //此处是和上面不同的

std::cout<<actualint<<std::endl;
std::cout<<otherint<<std::endl;

otherint++;
std::cout<<actualint<<std::endl;
std::cout<<otherint<<std::endl;

actualint++;
std::c

int& otherint =actualint
otherint是actualint的一个引用。你无论对那个变量进行操作,都会引起另一个变量的变化。他们两个操作的是用一块内存区域

int& otherint =actualint; 等于是给 actualint 取了一个小名,两个东西其实是一个,无论你在哪使用