Is there in C++ something similar to weak reference in Java ? I have list of objects in game (class Soldier, list is std::list* enemy), where I hold list of visible enemy soldiers. Every my soldier can have pointer to enemy Soldier ( Soldier* target; inside class). What I need when other mine soldiers killed enemy soldier (then killed soldier is removed from enemy list) I want that all my soldiers which have that pointer of soldier as target to have now null, because it is deleted. I can solve this with soldier's id and in every loop to check is there enemy soldier with same id but it seems like to brute force. Can I solve this on more elegant way( I cannot use c++11?
class Soldier{
Soldier* target;
public:
// other functions
void shootAtTarget();
};
A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist.
Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. Weak references are most often used to implement canonicalizing mappings. Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable.
Pointers that are not retained are often referred to as “weak” in Objective-C documentation that predates the garbage collector. These are references that are allowed to persist beyond the lifetime of the object. Unfortunately, there is no automatic way of telling whether they are still valid.
Strong References: This is the default type/class of Reference Object. Any object which has an active strong reference are not eligible for garbage collection. The object is garbage collected only when the variable which was strongly referenced points to null. MyClass obj = new MyClass ();
Note that there is no exact behavioral equivelent. Java's weak reference concept really relies on there being a garbage collector, and managed memory in general. In C++, a pointer is just a memory location, so something else needs to "track" the lifecycle of an object.
In Java, all memory is being managed by the garbage collector, so there is a mechanism to know when an object is removed. In C++, this doesn't happen without library usage, since the pointer itself is really just a number.
You should be able to use a std::weak_ptr
(or tr1::weak_ptr
) to get similar behavior. This requires storing your object in a std::shared_ptr
instead of a raw pointer, though (which is a good idea anyways).
Given that you cannot use C++11, you can use boost::weak_ptr
with boost::shared_ptr
instead of the C++ standard libraries.
It looks like you should use Observer design pattern
http://en.wikipedia.org/wiki/Observer_pattern .
If each soldier has a list of soldiers which target that soldier, you can just clear their targets when the soldier dies. You can also make the targeting soldiers try to acquire another target.
Since you can't user weak_ptr (as suggested in the other answer) I would just suggest checking if the target is dead (or deallocated/null) in your other Soldiers, and acting accordingly (which is kind of what the weak_ptr would've given you from a logical perspective, except you have to do the check explicitly).
Unless you can move to C++11 (a good idea). OR use the boost variant, as mentioned in another answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With