Jump to content
  • Advertisement

Archived

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

keethrus

casting pointer

This topic is 5653 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
Advertisement
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
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!