Archived

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

Linking objects through derived classes

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

Hey- Like just about everyone else, I want to link the objects in my game but the problem lies when I derive my Object class from a Node class or Tree class. I've read OpenGL game progaming and I'm linking my objects in a similar manner as they do in the book for the game engine-- making a Tree base class then deriving Objects form this. They use a whole bunch of pointer casts to cast the Tree class pointers to the Object class pointers. I think there's a better way. But My problem is, as I call a member funtion from the derived object class (say atatching one object to another through a funtion), somehow my pointer addresses get screwed up. Maybe not screwed up, but they don't point to where I expect them to.(It changes) In turn, it doesn't work. I know it has to do with the fact That I derived the Object class. I can't add the fuctionality of a tree through a derived class. How do you typicaly do this in your games? or rather, how do you link your objects together? This is what I really would like to know. -Thanks [edited by - ChrisLockhart on August 16, 2002 10:05:40 PM]

Share this post


Link to post
Share on other sites
I don't know if it will do what you need done, but I would stick with simple linked lists, and not use a base class for that reason alone. If you need to have multiple types in one list, just derive the types from a base class that includes default attributes, including the links to other nodes. example:
class BaseObj
{
public:
// attributes
BaseObj *L,*R; // or Previous,Next
// methods
void Draw();
};
class Item : public BaseObj
{
....
};
class Monster : public BaseObj
{
....
};


In that example, any of those types can be inserted and used from the same linked list. The code to insert, remove or deal with these lists are pretty simple, and I doubt deriving from a link type object would help much there.
BaseObj *FO=NULL,*LO=NULL; // Say these are the first & last list pointers
// It's totally cool to go something like..
Monster Freak;
FO = &Freak;
// The only time you will need to cast is if you need to get
// a pointer back to a monster from a BaseObj pointer.
Monster *FreakPtr = (Monster*) FO;
// perhaps in your drawing code
BaseObj *Obj = FO;
while(Obj)
{
Obj->Draw();
Obj = Obj->R;
}


Hopefully I wasn't way off from what you were asking, and sorry my logic is so messy and unorganized

Jiia

[edited by - Jiia on August 17, 2002 4:04:32 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by ChrisLockhart
They use a whole bunch of pointer casts to cast the Tree class pointers to the Object class pointers. I think there''s a better way.

This is actually a pretty good way. A better way might be to use STL containers.

quote:
But My problem is, as I call a member funtion from the derived object class (say atatching one object to another through a funtion), somehow my pointer addresses get screwed up. Maybe not screwed up, but they don''t point to where I expect them to.(It changes) In turn, it doesn''t work. I know it has to do with the fact That I derived the Object class.

Then you are, unfortunately, doing something wrong elsewhere. There''s no reason why the method they give you won''t work correctly. You''re not doing something like using memset or memcpy on the class, are you?

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share this post


Link to post
Share on other sites
quote:
Original post by ChrisLockhart
...But My problem is, as I call a member funtion from the derived object class (say atatching one object to another through a funtion), somehow my pointer addresses get screwed up. Maybe not screwed up, but they don''t point to where I expect them to.(It changes) In turn, it doesn''t work. I know it has to do with the fact That I derived the Object class.



Make sure the methods in the base class are declared virtual, and do the same in the derived classes.

It might help to show some code.


University is a fountain of knowledge, and students go there to drink.

Share this post


Link to post
Share on other sites