#include <iostream>
using namespace std;
int main()
{
//const& 使引用变量成为只读变量,不可直接更改,但可通过:改变引用的值 或 指针解引用后进行改变;
int a = 0;
const int& ca = a;
a = 1; //通过改变引用的具体变量值;
cout << ca << endl; //打印 1
const int b = 0; //b为常量,放入符号表;
const int& cb = b;
int* pb = (int*)&cb;
*pb = 1; //通过指针解引用进行改变;
cout << b << endl; //打印 0(b不变)
cout << cb << endl; //打印 1
const int& c = 0;
int* pc = (int*)&c;
*pc = 1; //通过指针解引用进行改变;
cout << c << endl; //打印 1
return 0;
}
const与引用小结
最新推荐文章于 2024-04-29 11:30:50 发布