Technology Tech
Technology Tech

Reputation: 11

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?

Upvotes: 1

Views: 1084

Answers (1)

Mark B
Mark B

Reputation: 96241

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.

Upvotes: 1

Related Questions