Archived

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

keethrus

casting pointer

Recommended Posts

keethrus    122
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
Zipster    2359
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
civguy    308
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
petewood    819
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
CWizard    127
or...

#include <list>

std::list<someClass> list;
...

list.front().Draw();


STL is your friend




[edited by - CWizard on January 29, 2003 5:14:41 AM]

Share this post


Link to post
Share on other sites