Void pointers and casting

Started by
7 comments, last by Evil Steve 16 years, 8 months ago
I've got a function that takes a void pointer as an argument (pParam) and in the body of the function I need to be able to cast the void pointer to a pointer of a base class and call one of the base classes member functions. I've tried all different ways of casting the pointer but the program is still throwing errors (Access violation). Any ideas?
Advertisement
stop passing it NULL?

Get off my lawn!

void foo( void *x ){  Bar *y = (Bar *)x;  // or Bar *y = reinterpret_cast< Bar * >(x);  y->foo();}


Access violation simply means something is wrong somewhere along the line. NULL pointer, invalid cast, etc, but you won't get much information as it is.
when it breaks out in the debugger, what's the value of the pointer?

-me
The following should work (untested):
struct Base{   void someFunc() {a = 42;}   int a;};struct Derived : public Base{   int b;};void Thingy(void* pParam){   Base* pBase = (Base*)pParam;   pBase->someFunc();}int main(){   Derived whatsit;   Thingy(&whatsit);   return 0;}


We'll need to see some code if that's not similar to what you have.
Quote:Original post by FunLogic
that takes a void pointer as an argument (pParam)


Why? If you know what to cast it to, why wouldn't you just accept that kind of pointer? (Hint: you can pass a derived pointer to a function accepting a base pointer with no extra typing. If the function is non-virtual, it will use the base version of the function with no extra typing, too.) And why not pass a reference instead? (Hint: if you *do* want to use virtual functions, they work fine with references, too, not just pointers.)
Thanks for all the replies

Quote:stop passing it NULL?


Nope, not the problem.

Quote:Why? If you know what to cast it to, why wouldn't you just accept that kind of pointer?


Because I didn't write, and cannot re-write the functions prototype.

Quote:We'll need to see some code if that's not similar to what you have.


In my code, the function in the base class is pure virtual, and is redefined by the derived class. (Perhaps I should have mentioned that to begin with).
Well, it's pretty simple: if you cast a Base* to a void*, you can get a Base* back with a reinterpret_cast.

If you cast a Derived* to a void*, you can get a Derived* back with a reinterpret_cast, but getting a Base* is potentially impossible (and it is often impossible in the case of multiple inheritance).

So, whether or not this is the problem, if your function performs a reinterpret_cast<Base*>(ptr) on its argument, make sure that you static_cast<Base*>(ptr) anything you might want to pass to that function, just in case.

Quote:Original post by FunLogic
In my code, the function in the base class is pure virtual, and is redefined by the derived class. (Perhaps I should have mentioned that to begin with).
Shouldn't make any difference. We'll still need to see some code; particularly how you cast from the void pointer to the base class pointer. Also, if you print out the value of the void pointer in the function, is it "sensible"?

This topic is closed to new replies.

Advertisement