Skip to main content
gamedev.net
🔒 Locked

C++ Pointer/Reference Question

Started by Maxamor Mar 15, 2007 at 9:01 PM 3 replies 850+ views
Original Post
Maxamor
Maxamor
This should be easy. My program is designed in such a way that all of the animations are loaded only once and cached. When a game object needs to use an animation, it gets a pointer to one from the "Animation Cache". If for some reason, my game object accidentaly calls delete on the pointer, that object is still cached but is now null. When another object requests a pointer to it, it will get a pointer to nothing. And in this process, problems arise. The easy answer is to not call delete on the pointer. However, is there a way that I could have the compiler see that I'm doing this and say "Hey! No!" ? A const pointer perhaps?
Crypter
Crypter
If your animations are preloaded, you can easily impliment a handle based
system that refrences the animations in the cache. This will solve all pointer
problems, and still have the effencency (For the most part) of pointers.

Also, if "main game object" can create animations (But not delete them), I
suspect a design issue, as this breaks encapsulation. Whoever is responsible
for creating animations (and adding them to the animation cache) should also
destroy them. Mabey create a manager for the animation cache?)

Cant really help you more because we dont know exactally what "Animation Cache"
is.

As Sneftel suggested, you really should not use raw pointers. I recommend
either creating a memory manager to track usage, and/or smart pointers.

Hope this helps[smile]
Maxamor
Maxamor
Thanks for the advice. I will give smart pointers a shot.

To clarify, my "animation cache" is just a place where animation data is stored so that game objects that need to play a specific animation can get a reference to it.

The animation cache creates and destroys all animations. References to the animations are requested by game objects for use.

This way, I don't have to have 100 copies of an animation object, I could have 100 game objects all referencing one animation, playing different frames of it, all at the same time.

I don't see how this model breaks encapulation.

This is the same concept as having a mesh object loaded only once but being reused through references in many objects concurrently.

Maybe my terminology needs adjustment.

Anyway, thanks again for the advice.
Crypter
Crypter
I see. Smart pointers seems it to be what you should use, then.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.