Sign in to follow this  

Void pointers and casting

This topic is 3771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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"?

Share this post


Link to post
Share on other sites

This topic is 3771 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this