Archived

This topic is now archived and is closed to further replies.

casting pointer

This topic is 5436 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 have a generik linked list where each node has a pointer of type void. how do i cast this void pointer as a class pointer? like: currentNode->(someClass *)ptr->Draw(); where "currentNode" is a pointer to a node. "ptr" is the void pointer in the node. and "Draw" is a function of the class "someClass". (in this line im assuming "ptr" points to a "someClass" class.) - jeremiah inlovewithGod.com

Share this post


Link to post
Share on other sites
Should be small matter of getting the syntax right. Without being able to test it out, I''d say it would look something like this:

currentNode->((someClass*)ptr)->Draw();

Plus or minus a set of parenthesis here and there, maybe. It''s all about the parenthesis

Share this post


Link to post
Share on other sites
I think it''s like this:
(someClass *)(currentNode->ptr)->Draw();

currentNode->ptr returns the void pointer which is then cast to someClass*-type and then Draw() is invoked.

Use C++-style casts:
reinterpret_cast<someClass*>(currentNode->ptr)->Draw();

Share this post


Link to post
Share on other sites
if you know that everything in the list will be a certain type of thing then store pointers to that thing, not void. That''s something templates are useful for.

Share this post


Link to post
Share on other sites