简体   繁体   中英

How to check if Void pointer can be converted to other specific pointer type?

I have a method that has a void pointer as argument. In the body of the method I want to write some code that should execute only if the void pointer is convertible to a specific type of pointer(in my case it is a card*). How do I check if the card is convertible to card pointer?

There is no C++ language feature to do this.

[Don't do this] One possible approach is to make the void* point to a class of a specific known type that stores type information and another void* . Then you can convert the first void* to the type-containing data type, determine if the type is what you want, and then take the nested void* and cast it to the desired type.

All that said, what's the real problem you're trying to solve? There's probably a C++-idiomatic approach to that .

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