forked from boostorg/smart_ptr
@ -29,20 +29,24 @@
|
|||||||
and <STRONG>shared_ptr<T const></STRONG>, depending on constness, to <STRONG>this</STRONG>.</P>
|
and <STRONG>shared_ptr<T const></STRONG>, depending on constness, to <STRONG>this</STRONG>.</P>
|
||||||
<h3><a name="Example">Example</a></h3>
|
<h3><a name="Example">Example</a></h3>
|
||||||
<pre>
|
<pre>
|
||||||
class Y: public enable_shared_from_this<Y>
|
#include <boost/enable_shared_from_this.hpp>
|
||||||
|
#include <boost/shared_ptr.hpp>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
class Y: public boost::enable_shared_from_this<Y>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
shared_ptr<Y> f()
|
boost::shared_ptr<Y> f()
|
||||||
{
|
{
|
||||||
return shared_from_this();
|
return shared_from_this();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
shared_ptr<Y> p(new Y);
|
boost::shared_ptr<Y> p(new Y);
|
||||||
shared_ptr<Y> q = p->f();
|
boost::shared_ptr<Y> q = p->f();
|
||||||
assert(p == q);
|
assert(p == q);
|
||||||
assert(!(p < q || q < p)); // p and q must share ownership
|
assert(!(p < q || q < p)); // p and q must share ownership
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user