zxj3791

导航

C++ || const_cast 将const变量转为非const

点击查看代码
#include <iostream>
using namespace std;



int main()
{	
	int a =5;
	const int* p=&a;//需要用&a,对应左边的 指针变量
	cout<<"a的值:"<<a<<endl;
	cout<<"a的地址:"<<&a<<endl;
	cout<<"p指针指代的地址:"<<p<<endl; 
	int* temp = const_cast<int*>(p);
	*temp =24;
	cout<<"p指针所指变量的值:"<<*p<<endl;
	cout<<"a的值:"<<a<<endl;
	
	return 0;
}

posted on 2022-04-16 17:22  有序  阅读(181)  评论(0编辑  收藏  举报