简体   繁体   中英

c++: How do I safely cast a const double** to a const void**

The following code gives a compiler error in C++:

const double** x;
const void** y = x;

How do you get a const-safe equivalent?

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM