Boost logo

Boost :

From: Jonathan Wakely (cow_at_[hidden])
Date: 2005-06-06 10:04:39


On Mon, Jun 06, 2005 at 03:58:41PM +0200, Lorenzo Bettini wrote:

> However, it is not clear to me how to use weak_ptr in this context...
>
> can some one provide some clue, please?

Well, for this context:

    class C;

    typedef boost::shared_ptr<C> CP;
    typedef boost::weak_ptr<C> WP;

    class C
    {
    private:
      string s;
      WP wp;

    public:
      C(const string &ss, CP c = CP()) : s(ss), wp(c) {}
      void set(CP c) { wp = c; }
      ~C() { cout << "destroying C" << endl; }
    };

Then main() would be unchanged.

That might not work for anything less trivial than your example, since
you'd need to ensure the shared_ptrs that own the objects are still
around when you use the weak_ptrs.

jon


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk