I'm developing a game for a course at my school. One of the assignments is to enable saving the game to a file and later load the game from the same file.
The problem I'm having are pointers. I'm not allocating anything on the stack (due to ownership of an item for example) so all the classes have pointers to whatever they want a reference to.
While this is quite easy to solve (assign an ID to each object and store that id instead of the pointer) the real problem comes with multiple inheritance.
Let's take the class Actor for example. It looks like this: class Actor : public Object, public ItemOwner
where Object
is a Obj-C-style base class which has a retain count and release, retain and autorelease methods. ItemOwner is simply an Interface which has some methods such as virtual bool add(Item *item) = 0;
, virtual void remove(Item *item) = 0;
and virtual bool transfer_ownership(Item *item, ItemOwner *new_owner) = 0;
Now the real question comes, which class(es?) should have ID's. And Item has a pointer to an ItemOwner while a Room has a pointer to an Actor. The actor should only be saved once.
I've thought about assigning ID's to each superclass (Object, ItemOwner, etc) but if I have a pointer to an Actor will that actor always have the same adress as the Object it contains (Actor *foo = new Actor(); foo == (Object *)foo)
? If not every single class in the game will need to have an ID.
Any thoughts or suggestions would be greatly appreciated.