Class/inheritance problem

Started by
20 comments, last by Programmer16 19 years, 8 months ago
Quote:Original post by ze_jackal
Quote:Original post by tomek_zielinski
In this case, though, they are perfect - each loop iteration I can chceck if zombie reference counter is equal to 1. If it is, then zombie was deleted from main list and remains only in zombie-specific list, so I can delete it from this list too.

<a href="http://www.boost.org/libs/smart_ptr/weak_ptr.htm>boost::weak_ptr
. A weak_ptr is created from a shared ptr, and can be stored in a container. It acts as an observer of the shared_ptr, but it can't directly use it.

To use the shared_ptr, you have to call weak_ptr.lock(), which returns a valid shared_ptr. However, if the shared_ptr that the weak_ptr refers to does not exist, the lock will fail by returning an empty shared_ptr.

So: keep one master list of shared_ptr<Object>, and a bunch of lists such as weak_ptr<Soldier>, weak_ptr<Zombie>, etc. To remove an object, remove it from the master list. When you iterate over the other lists, any objects that have been removed from the master will fail to lock, and can be removed from the secondary list.


Oh man, you're my saviour here:) I was always considering using weak_ptrs for a number of things but never comprehend them well.

Basically using weak_ptr's would be almost the same to using share_ptr's except that when soldier is removed from main list it would be deleted instantly, the deletion wouldn't be deffered. In my program such deffering wouldn't be a problem, I know precisely lifetime of my objects, but weak_ptrs are more robust in the long run.

Thx!
www.tmreality.com
Advertisement
Sorry if this was already posted, I didn't have enough time to read everybody's posts. I (am going to) implement it this way:

1) You have an enum of actions (ACT_RUN, ACT_BITE, ACT_STRAFE)
2) Your base class CSceneCharacter has a function called OnAction.
3) Instead of calling OnStrafe(), call OnAction(ACT_STRAFE). If the character doesn't use that action, the function just returns.

Hopefully that helps!

This topic is closed to new replies.

Advertisement