c++ (常量的引用)

c++(常量的引用)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;
void test01()
{
	//int &ref=10; //引用了不合法的内存,不可以
	const int &ref = 10; //加上const后 编译器处理方式为: int tmp = 10 const int &ref=tmp;
	int *p = (int *)&ref;
	*p = 1000;
	cout << "ref = " << ref << endl;
}
//常量引用使用场景,用来修饰形参
void showValue(const int &val) {//如果内容只想显示不想修改,而不修改内容
	cout << "a = " << val<<endl;
}
int main()
{
	int a = 10;
	showValue(a);
	test01();
	return 0;
}

posted on 2021-04-21 19:06  lodger47  阅读(52)  评论(0)    收藏  举报

导航