C++ and reinterpret_cast

Started by
7 comments, last by Xai 17 years, 10 months ago
Hi Everyone, I have this code in my engine and the list pointer is messing up at the reinterpret_cast line. I was wondering if anyone can tell me what I've done wrong. Am I using reinterpret_cast incorrectly? If so what should I change it to? Thanks! :) class ENTITY : public SCREEN_OBJECT, public WORLD_OBJECT, public FILE_OBJECT { // class stuff here }; class WORLD_OBJECT { // class stuff here WORLD_OBJECT* AddObject (WORLD_OBJECT* list); }; WORLD_OBJECT* WORLD_OBJECT::AddObject (WORLD_OBJECT* list) { if (!list) return this; otherwise do other things... } ******************************************************************** The crashing code is below ******************************************************************** ENTITY* list = NULL,* e; // create e and fill it with data... // add e to list list = reinterpret_cast <ENTITY*> (e->AddObject (list)); // it crashes when add object returns this.
"I am a pitbull on the pantleg of opportunity."George W. Bush
Advertisement
ENTITY* list = NULL,* e;

What type is e?
did you mean
ENTITY* list,* e = NULL;
You're using reinterpret_cast incorrectly. You should be using dynamic_cast instead (or better, a different design entirely). dynamic_cast is for navigating inheritance hierarchies. reinterpret_cast is for those rare occasions when you need to circumvent the type system.

Σnigma
Thanks for that Enigma. It did the trick.

I really need to get up to speed on this stuff. :)
"I am a pitbull on the pantleg of opportunity."George W. Bush
const_cast to change constness (usually just to remove since adding is automatic)

dynamic_cast to move between class pointers with common bases (returns null if the object is not actually of the type in question)

static_cast to perform type conversion (think situations where the data changes such as float to int) - compile time error if the desired conversion operation is not defined.

reinterpret_cast to ignore the original type and simply view its data as a new type (think writing an int out in binary using a char* like reinterpret_cast<char*>(&myInt), and also think of this as an after-the-fact replacement for the C union construct).
I do hope you're not inventing your own linked list (I know I infer things from the variable name)? Use std::list instead (or some other library container: most people who write their own linked lists aren't even aware of other strategies for implementing resizable containers, IMX).
Yeah this is some old code from a Vis C++ 5.0 compiler. STL never worked right on 5.0, so the code has linked lists in it. :(
"I am a pitbull on the pantleg of opportunity."George W. Bush
VC++ *5*? Even 6 is considered pathetically ancient. Please welcome yourself to the new millenium and read up on the current C++ standard library. :)
Quote:Original post by Zahlman
I do hope you're not inventing your own linked list (I know I infer things from the variable name)? Use std::list instead (or some other library container: most people who write their own linked lists aren't even aware of other strategies for implementing resizable containers, IMX).


I think everyone should invent their own linked list ... while learning. And use it not more than twice (in the test program, and maybe once afterward to realize how limited it really is, and why the STL version is so great).

Not for any reason other than the same reason we recmomend tetris, tic-tac-toe and space invaders to new game programmers. They have nothing to add to this area of programming, but it has something to add to them.

This topic is closed to new replies.

Advertisement