|
Boost : |
Subject: Re: [boost] [shared_ptr] delete shared_ptr
From: Christoph Heindl (christoph.heindl_at_[hidden])
Date: 2011-03-17 05:44:41
On Wed, Mar 16, 2011 at 8:23 PM, pavel novikov <paul.cpprules_at_[hidden]> wrote:
> i'm excited to present you a technique which i occasionally invented
> shared pointers are very good helpers
yes they are. While this topic is hot, I'd like to add a use-case for
shared_ptr that is probably not too obvious:
Assume the following class interface:
// A class that works with points. In order to do so, it requires an
accelerator data structure to speed up computations.
// Building an accelerator is costly and should be avoided if such an
accelerator over the points has already been built.
class works_with_points {
// Construct from points. Need to generate accelerator.
works_with_points (const array_of_points &points)
: _acc(new accelarator_over_points(points)) {}
// Construct from pre-existing accelerator.
// Accelerator is guaranteed to live as long as this class instance does.
works_with_points (boost::shared_ptr<accelarator_over_points const> accel)
: _acc(accel) {}
// Construct from pre-existing accelerator.
works_with_points (const accelarator_over_points &accel)
: _acc(&accel, null_deleter()) {}
private:
boost::shared_ptr<accelarator_over_points const> _acc;
};
Best regards,
Christoph
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk