Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-10-02 07:28:24


From: "David Abrahams" <dave_at_[hidden]>
>
> struct intrusive_count
> {
> intrusive_count() : count(new counted_base) {}
> ~intrusive_count() { if (count->weak_count == 0) delete count; }
> counted_base* count;
> };

Incidentally, this reminds me of "shared_from_this solution #18" - having
the object store a weak_ptr to itself. The interesting thing is that this is
a user-space solution, as it doesn't require help from shared_ptr.

class X
{
private:

    weak_ptr<X> weak_this;

    X(); // only create on heap

    X(X const &); // noncopyable
    X & operator=(X const &);

public:

    static shared_ptr<X> create()
    {
        shared_ptr<X> px(new X);
        px->weak_this = px;
        return px;
    }

    shared_ptr<X> shared_from_this()
    {
        return shared_ptr<X>(weak_this);
    }

    shared_ptr<X const> shared_from_this() const
    {
        return shared_ptr<X const>(weak_this);
    }
};


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