4

I've found QPointer. Are there any others?

demonplus
  • 5,613
  • 12
  • 49
  • 68
Neil G
  • 32,138
  • 39
  • 156
  • 257

2 Answers2

8

Boost - the weak_ptr has some nice features that make it safe to use, if you are also using shared_ptr. You keep a weak_ptr reference to an instance that is managed by shared_ptr lifetime. When you need to use the underlying instance, convert it to a shared_ptr instance using the constructor of the shared_ptr class, or the lock method. The operation will fail if the underlying instance was deleted. The use is thread safe in the same fashion as the shared_ptr class:

shared_ptr<int> p(new int(5));
weak_ptr<int> q(p);

// some time later

if(shared_ptr<int> r = q.lock())
{
    // use *r
}
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
  • Thanks. I wasn't aware that weak_ptr had this behaviour. I wonder how much I'm paying for the unneeded reference counting behaviour though. Anyway, your answer was first, I think. Marking as best. – Neil G May 26 '09 at 17:55
6

"boost::weak_ptr" works really well with "boost::shared_ptr" (also available in tr1)

stefaanv
  • 14,072
  • 2
  • 31
  • 53