I have two classes, A, and B. A is a parent class of B, and I have a function that takes in a pointer to a class of type A, checks if it is also of type B, and if so will call another function that takes in a pointer to a class of type B. When the function calls the other function, I supply reinterpret_cast(a) as the parameter. If this seems ambiguous here is a code example:
void abc(A * a) {
if (a->IsA("B")) { //please dont worry much about this line,
//my real concern is the reinterpret_cast
def(reinterpret_cast<B *>(a));
};
};
So now that you know how I am calling "def", I am wondering if reinterpret_cast actually returns a pointer of type B to be sent off as the parameter to def. I would appreciate any help. Thanks
reinterpret_cast will always do what you say - it is a sledghammer. You can do
def(reinterpret_cast<B *>(42));
or
std::string hw = "hello";
def(reinterpret_cast<B *>(hw));
it will always return a pointer that might point at the correct type. It assumes you know what you are doing
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With