Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / smart_ptr / enable_shared_from_this.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <html>
3     <head>
4         <title>enable_shared_from_this</title>
5         <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6     </head>
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>
11         <p>
12             The header <STRONG>&lt;boost/enable_shared_from_this.hpp&gt;</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.
16         </p>
17         <P><STRONG>enable_shared_from_this&lt;T&gt;</STRONG> defines two member functions 
18             called <STRONG>shared_from_this</STRONG> that return a <STRONG>shared_ptr&lt;T&gt;</STRONG>
19             and <STRONG>shared_ptr&lt;T const&gt;</STRONG>, depending on constness, to <STRONG>this</STRONG>.</P>
20         <h2><a name="Example">Example</a></h2>
21         <pre>
22 #include &lt;boost/enable_shared_from_this.hpp&gt;
23 #include &lt;boost/shared_ptr.hpp&gt;
24 #include &lt;cassert&gt;
25
26 class Y: public boost::enable_shared_from_this&lt;Y&gt;
27 {
28 public:
29
30     boost::shared_ptr&lt;Y&gt; f()
31     {
32         return shared_from_this();
33     }
34 };
35
36 int main()
37 {
38     boost::shared_ptr&lt;Y&gt; p(new Y);
39     boost::shared_ptr&lt;Y&gt; q = p-&gt;f();
40     assert(p == q);
41     assert(!(p &lt; q || q &lt; p)); // p and q must share ownership
42 }
43 </pre>
44         <h3><a name="Synopsis">Synopsis</a></h3>
45         <pre>
46 namespace boost
47 {
48
49 template&lt;class T&gt; class enable_shared_from_this
50 {
51 public:
52
53     shared_ptr&lt;T&gt; shared_from_this();
54     shared_ptr&lt;T const&gt; shared_from_this() const;
55 }
56
57 }
58 </pre>
59         <h4>template&lt;class T&gt; shared_ptr&lt;T&gt; 
60             enable_shared_from_this&lt;T&gt;::shared_from_this();</h4>
61         <h4>template&lt;class T&gt; shared_ptr&lt;T const&gt; 
62             enable_shared_from_this&lt;T&gt;::shared_from_this() const;</h4>
63         <blockquote>
64             <p>
65                 <b>Requires:</b> <STRONG>enable_shared_from_this&lt;T&gt;</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>
69                 <STRONG>t</STRONG>.
70             </p>
71             <p>
72                 <b>Returns:</b> A <b>shared_ptr&lt;T&gt;</b> instance <b>r</b> that shares 
73                 ownership with <b>p</b>.
74             </p>
75             <p>
76                 <b>Postconditions:</b> <tt>r.get() == this</tt>.
77             </p>
78         </blockquote>
79         <p>$Date$</p>
80         <p>
81             <small>Copyright &copy; 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>
84     </body>
85 </html>