I\'m looking for a proper name of a class template with the following features:
ID: 658937 • Letter: I
Question
I'm looking for a proper name of a class template with the following features:
It's interface would look something like this, where Foo is a placeholder for the name I'm looking for.
template<typename T>
struct Foo
{
T* operator->();
T* operator*();
}
So, it imitates a pointer to an object, much like std::unique_ptr<> and std::shared_ptr<> do. Unlike them, having a Foo<T> to an object doesn't imply ownership over this object. Everything Foo<T> does is provide a pointer to this object and, this is the important part, once the object Foo<T> points to is deleted both operators return nullptr instead of the now invalid pointer.
I'm looking for a descriptive name for this class. Maybe it has intersecting semantics with a well defined pattern I am not aware of?
Explanation / Answer
It's a weak pointer.
It's common in GC languages for cached values that are allowed to be deleted and the map that holds it can detect it and recreate it.
C++ has a weak_ptr that works in conjunction with shared_ptr. You can assign a shared_ptr to a weak_ptr and get the shared_ptr from it again later with lock as long as the last shared_ptr hasn't been destroyed yet, after it has then lock returns a shared_ptr that holds a null pointer.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.