casting pointer

Started by
3 comments, last by keethrus 21 years, 2 months ago
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
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
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();
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.
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]

This topic is closed to new replies.

Advertisement