[英]c++: How do I safely cast a const double** to a const void**
The following code gives a compiler error in C++: 以下代码给出了C ++中的编译器错误:
const double** x;
const void** y = x;
How do you get a const-safe equivalent? 您如何获得const安全等效项?
Of course, you can get this to work with a simple cast: 当然,您可以通过简单的强制转换来使其工作:
const void** y = (const void**) x;
But surely the compiler should know that this ok? 但是肯定编译器应该知道这一点吗? Why does it complain?
为什么会抱怨?
Why should the compiler know that that is OK? 为什么编译器应该知道那可以? I think you want the following instead
我想你想要以下内容
void *y = x;
x = static_cast<const double**>(y); // casting back needs static_cast or c-style cast
A void**
doesn't have the special properties that a void*
has (that of being an universal data pointer). void**
没有void*
具有的特殊属性(作为通用数据指针)。
Why does it complain? 为什么会抱怨?
Because it's NOT OK. 因为那不行。
There's a FAQ explaining why but I can't seem to find it right now. 有一个常见问题解答解释了为什么,但我现在似乎找不到。
Your C-Style cast resolves to a reinterpret_cast, which tells the compiler to ignore types. 您的C-Style强制类型转换为reinterpret_cast,它告诉编译器忽略类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.