Sign in to follow this  

C++ and reinterpret_cast

This topic is 4199 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

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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ENTITY* list = NULL,* e;

What type is e?
did you mean
ENTITY* list,* e = NULL;

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
VC++ *5*? Even 6 is considered pathetically ancient. Please welcome yourself to the new millenium and read up on the current C++ standard library. :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4199 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this