File: enable_shared_from_this.html

package info (click to toggle)
boost 1.33.1-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 100,948 kB
  • ctags: 145,103
  • sloc: cpp: 573,492; xml: 49,055; python: 15,626; ansic: 13,588; sh: 2,099; yacc: 858; makefile: 660; perl: 427; lex: 111; csh: 6
file content (93 lines) | stat: -rw-r--r-- 3,080 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<title>Boost: enable_shared_from_this.hpp documentation</title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
	</head>
	<body bgcolor="white" style="MARGIN-LEFT: 5%; MARGIN-RIGHT: 5%">
		<table border="0" width="100%">
			<tr>
				<td width="277">
					<img src="../../boost.png" alt="boost.png (6897 bytes)" width="277" height="86">
				</td>
				<td align="middle">
					<h1>enable_shared_from_this.hpp</h1>
				</td>
			</tr>
			<tr>
				<td colspan="2" height="64">&nbsp;</td>
			</tr>
		</table>
		<h3><a name="Purpose">Purpose</a></h3>
		<p>
			The header <STRONG>&lt;boost/enable_shared_from_this.hpp&gt;</STRONG> defines 
			the class template <STRONG>enable_shared_from_this</STRONG>. It is used as a 
			base class that allows a <A href="shared_ptr.htm">shared_ptr</A> to the current 
			object to be obtained from within a member function.
		</p>
		<P><STRONG>enable_shared_from_this&lt;T&gt;</STRONG> defines two member functions 
			called <STRONG>shared_from_this</STRONG> that return a <STRONG>shared_ptr&lt;T&gt;</STRONG>
			and <STRONG>shared_ptr&lt;T const&gt;</STRONG>, depending on constness, to <STRONG>this</STRONG>.</P>
		<h3><a name="Example">Example</a></h3>
		<pre>
class Y: public enable_shared_from_this&lt;Y&gt;
{
public:

    shared_ptr&lt;Y&gt; f()
    {
        return shared_from_this();
    }
}

int main()
{
    shared_ptr&lt;Y&gt; p(new Y);
    shared_ptr&lt;Y&gt; q = p-&gt;f();
    assert(p == q);
    assert(!(p &lt; q || q &lt; p)); // p and q must share ownership
}
</pre>
		<h3><a name="Synopsis">Synopsis</a></h3>
		<pre>
namespace boost
{

template&lt;class T&gt; class enable_shared_from_this
{
public:

    shared_ptr&lt;T&gt; shared_from_this();
    shared_ptr&lt;T const&gt; shared_from_this() const;
}

}
</pre>
		<h4>template&lt;class T&gt; shared_ptr&lt;T&gt; 
			enable_shared_from_this&lt;T&gt;::shared_from_this();</h4>
		<h4>template&lt;class T&gt; shared_ptr&lt;T const&gt; 
			enable_shared_from_this&lt;T&gt;::shared_from_this() const;</h4>
		<blockquote>
			<p>
				<b>Requires:</b> <STRONG>enable_shared_from_this&lt;T&gt;</STRONG> must be an 
				accessible base class of <b>T</b>. <STRONG>*this</STRONG> must be a subobject 
				of an instance <STRONG>t</STRONG> of type <STRONG>T</STRONG> . There must exist 
				at least one <STRONG>shared_ptr</STRONG> instance <STRONG>p</STRONG> that <EM>owns</EM>
				<STRONG>t</STRONG>.
			</p>
			<p>
				<b>Returns:</b> A <b>shared_ptr&lt;T&gt;</b> instance <b>r</b> that shares 
				ownership with <b>p</b>.
			</p>
			<p>
				<b>Postconditions:</b> <tt>r.get() == this</tt>.
			</p>
		</blockquote>
		<p>
			<br>
			<small>Copyright  2002, 2003 by Peter Dimov. Permission to copy, use, modify, sell 
				and distribute this document is granted provided this copyright notice appears 
				in all copies. This document is provided "as is" without express or implied 
				warranty, and with no claim as to its suitability for any purpose.</small></p>
	</body>
</html>