Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there in C++ something similar to weak reference in Java?

Tags:

c++

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();
};
like image 542
PaolaJ. Avatar asked May 17 '13 16:05

PaolaJ.


People also ask

What is weak reference in C?

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.

What is Java weak reference?

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.

What is a weak reference Objective C?

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.

What is strong reference in Java?

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 ();


4 Answers

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.

like image 171
Reed Copsey Avatar answered Sep 24 '22 03:09

Reed Copsey


It looks like you should use Observer design pattern http://en.wikipedia.org/wiki/Observer_pattern .

like image 43
IProblemFactory Avatar answered Sep 26 '22 03:09

IProblemFactory


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.

like image 33
nurettin Avatar answered Sep 27 '22 03:09

nurettin


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.

like image 40
SubSevn Avatar answered Sep 24 '22 03:09

SubSevn