#include <iostream>
using namespace std;
void printBuf(const char* p) {//只读属性
//p[0] = '1';// error 不可修改
cout << "p: " << p << endl;
char* p1 = NULL;
p1 = const_cast<char*>(p);//注意:必须确保p所指向的空间确实能修改,否则会有灾难性错误
p1[0] = 'z';
cout << "p1: " << p1 << endl;;
}
int main(){
//const_cast类型转换
char buf[] = "11111111111aaaaaaaaaaa";//分配内存
printBuf(buf);
char* mp = "123456";//未分配内存 内存不能修改
printBuf(mp);//报错
system("pause");
return 0;
}
const_cast类型转换
最新推荐文章于 2023-08-11 21:19:02 发布