casting pointer
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
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:
Plus or minus a set of parenthesis here and there, maybe. It''s all about the parenthesis
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();
(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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement