首页 > 其他分享 >常类型转换:const_cast

常类型转换:const_cast

时间:2023-03-12 10:44:38浏览次数:29  
标签:类型转换 const ref1 cast 移除 ref

常类型转换:const_cast

// 用于移除对象的const属性
// 通常不是为了修饰对象,而是为了给函数传参时函数可以接受该对象
const int& ref = 1;  // ref的值不可修饰
int& ref1 = const_cast<int&>(ref);  // 将常属性移除
ref1 = 20;  // 可以通过ref1间接修改ref的值
cout << ref << endl;  // 结果为20

标签:类型转换,const,ref1,cast,移除,ref
From: https://www.cnblogs.com/kxwslmsps/p/17207737.html

相关文章