Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2002-09-21 20:48:57


From: "David Abrahams" <dave_at_[hidden]>
> If you derive from counted_base, what happens with weak_ptr?
> It seems as though every live weak_ptr ends up keeping the object
alive...

Yup, this demonstrates:

    #include <iostream>
    #include <boost/shared_ptr.hpp>
    #include <boost/weak_ptr.hpp>

    using namespace boost;
    using namespace std;

    struct foo
    {
        ~foo() { cout << "~foo"; }
    };

    struct bar : counted_base
    {
        ~bar() { cout << "~bar"; }
    };

    int main()
    {
        weak_ptr<foo> f;
        weak_ptr<bar> b;

        {
            shared_ptr<foo> f2(new foo);
            f = f2;
            cout << "foo< ";
        }
        cout << " > foo\n";

        {
            shared_ptr<bar> b2(new bar);
            b = b2;
            cout << "bar< ";
        }
        cout << "> bar\n";
    }

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave_at_[hidden] * http://www.boost-consulting.com


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