C++ 静态\u将整数地址转换为指针
为什么你需要一个C风格的演员C++ 静态\u将整数地址转换为指针,c++,casting,C++,Casting,为什么你需要一个C风格的演员 int* ptr = static_cast<int*>(0xff); // error: invalid static_cast from type 'int' // to type 'int*' int* ptr = (int*) 0xff; // ok. int*ptr=static_cast(0xff);//错误:类型“int”的静态\u转换无效 //要键入“int*”
int* ptr = static_cast<int*>(0xff); // error: invalid static_cast from type 'int'
// to type 'int*'
int* ptr = (int*) 0xff; // ok.
int*ptr=static_cast(0xff);//错误:类型“int”的静态\u转换无效
//要键入“int*”
int*ptr=(int*)0xff;//好啊
在将整数转换为指针时,您需要使用C样式转换,或者直接使用重新解释转换
它的意思,因为标准对不相关的类型是这样规定的
标准要求这些演员在那里,因为
静态\u强制转换
只能在两个相关的类型之间进行强制转换。整数与指针无关,反之亦然,因此您需要使用重新解释\u cast
,它告诉编译器重新解释整数的位,就像它们是指针一样(反之亦然):
int*ptr=reinterpret\u cast(0xff);
有关更多详细信息,请阅读以下内容:
您还可以使用C风格演员最终将使用的
重新解释演员阵容。不过还是不是个好主意。
int* ptr = reinterpret_cast<int*>(0xff);