Boost logo

Boost Users :

Subject: [Boost-users] boost::enable_shared_from_this why?
From: Germán Diago (germandiago_at_[hidden])
Date: 2009-01-15 05:27:26


Hello, reading the documentation for enable_shared_from_this,
I can't understand why this kind of base class is needed. In
the example code in the documentation, we have:

class Y: public enable_shared_from_this<Y>
{
public:

    shared_ptr<Y> f()
    {
        return shared_from_this();
    }
}

int main()
{
    shared_ptr<Y> p(new Y);
    shared_ptr<Y> q = p->f();
    assert(p == q);
    assert(!(p < q || q < p)); // p and q must share ownership
}

Isn't this piece of code equivalent (since the two pointers share
ownership):

class Y
{
};

int main(int argc, char * argv[])
{
   shared_ptr<Y> p(new Y);
  shared_ptr<Y> q(p);
  ...

}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net