1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <title>enable_shared_from_this</title>
5 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
7 <body text="#000000" bgcolor="#ffffff" link="#0000ff" vlink="#0000ff">
8 <h1><img height="86" alt="boost.png (6897 bytes)" src="../../boost.png"
9 width="277" align="middle" border="0">enable_shared_from_this</h1>
10 <h2><a name="Purpose">Purpose</a></h2>
12 The header <STRONG><boost/enable_shared_from_this.hpp></STRONG> defines
13 the class template <STRONG>enable_shared_from_this</STRONG>. It is used as a
14 base class that allows a <A href="shared_ptr.htm">shared_ptr</A> to the current
15 object to be obtained from within a member function.
17 <P><STRONG>enable_shared_from_this<T></STRONG> defines two member functions
18 called <STRONG>shared_from_this</STRONG> that return a <STRONG>shared_ptr<T></STRONG>
19 and <STRONG>shared_ptr<T const></STRONG>, depending on constness, to <STRONG>this</STRONG>.</P>
20 <h2><a name="Example">Example</a></h2>
22 #include <boost/enable_shared_from_this.hpp>
23 #include <boost/shared_ptr.hpp>
24 #include <cassert>
26 class Y: public boost::enable_shared_from_this<Y>
30 boost::shared_ptr<Y> f()
32 return shared_from_this();
38 boost::shared_ptr<Y> p(new Y);
39 boost::shared_ptr<Y> q = p->f();
41 assert(!(p < q || q < p)); // p and q must share ownership
44 <h3><a name="Synopsis">Synopsis</a></h3>
49 template<class T> class enable_shared_from_this
53 shared_ptr<T> shared_from_this();
54 shared_ptr<T const> shared_from_this() const;
59 <h4>template<class T> shared_ptr<T>
60 enable_shared_from_this<T>::shared_from_this();</h4>
61 <h4>template<class T> shared_ptr<T const>
62 enable_shared_from_this<T>::shared_from_this() const;</h4>
65 <b>Requires:</b> <STRONG>enable_shared_from_this<T></STRONG> must be an
66 accessible base class of <b>T</b>. <STRONG>*this</STRONG> must be a subobject
67 of an instance <STRONG>t</STRONG> of type <STRONG>T</STRONG> . There must exist
68 at least one <STRONG>shared_ptr</STRONG> instance <STRONG>p</STRONG> that <EM>owns</EM>
72 <b>Returns:</b> A <b>shared_ptr<T></b> instance <b>r</b> that shares
73 ownership with <b>p</b>.
76 <b>Postconditions:</b> <tt>r.get() == this</tt>.
81 <small>Copyright © 2002, 2003 by Peter Dimov. Distributed under the Boost Software License, Version
82 1.0. See accompanying file <A href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or
83 copy at <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>.</small></p>