]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 <code><boost/enable_shared_from_this.hpp></code> defines | |
13 | the class template <code>enable_shared_from_this</code>. It is used as a | |
14 | base class that allows a <a href="shared_ptr.htm">shared_ptr</a> or | |
15 | a <a href="weak_ptr.htm">weak_ptr</a> to the current object to be obtained | |
16 | from within a member function. | |
17 | </p> | |
18 | <p><code>enable_shared_from_this<T></code> defines two member functions | |
19 | called <code>shared_from_this</code> that return a <code>shared_ptr<T></code> | |
20 | and <code>shared_ptr<T const></code>, depending on constness, to <code>this</code>. | |
21 | It also defines two member functions called <code>weak_from_this</code> that return | |
22 | a corresponding <code>weak_ptr</code>. | |
23 | </p> | |
24 | <h2><a name="Example">Example</a></h2> | |
25 | <pre> | |
26 | #include <boost/enable_shared_from_this.hpp> | |
27 | #include <boost/shared_ptr.hpp> | |
28 | #include <cassert> | |
29 | ||
30 | class Y: public boost::enable_shared_from_this<Y> | |
31 | { | |
32 | public: | |
33 | ||
34 | boost::shared_ptr<Y> f() | |
35 | { | |
36 | return shared_from_this(); | |
37 | } | |
38 | }; | |
39 | ||
40 | int main() | |
41 | { | |
42 | boost::shared_ptr<Y> p(new Y); | |
43 | boost::shared_ptr<Y> q = p->f(); | |
44 | assert(p == q); | |
45 | assert(!(p < q || q < p)); // p and q must share ownership | |
46 | } | |
47 | </pre> | |
48 | <h2><a name="Synopsis">Synopsis</a></h2> | |
49 | <pre> | |
50 | namespace boost | |
51 | { | |
52 | ||
53 | template<class T> class enable_shared_from_this | |
54 | { | |
55 | public: | |
56 | ||
57 | shared_ptr<T> shared_from_this(); | |
58 | shared_ptr<T const> shared_from_this() const; | |
59 | ||
60 | weak_ptr<T> weak_from_this() noexcept; | |
61 | weak_ptr<T const> weak_from_this() const noexcept; | |
62 | } | |
63 | ||
64 | } | |
65 | </pre> | |
66 | <h4><code>template<class T> shared_ptr<T> | |
67 | enable_shared_from_this<T>::shared_from_this();</code></h4> | |
68 | <h4><code>template<class T> shared_ptr<T const> | |
69 | enable_shared_from_this<T>::shared_from_this() const;</code></h4> | |
70 | <blockquote> | |
71 | <p> | |
72 | <b>Requires:</b> <code>enable_shared_from_this<T></code> must be an | |
73 | accessible base class of <code>T</code>. <code>*this</code> must be a subobject | |
74 | of an instance <code>t</code> of type <code>T</code>. | |
75 | </p> | |
76 | <p> | |
77 | <b>Returns:</b> If a <code>shared_ptr</code> instance <code>p</code> that <em>owns</em> | |
78 | <code>t</code> exists, a <code>shared_ptr<T></code> instance <code>r</code> that shares | |
79 | ownership with <code>p</code>. | |
80 | </p> | |
81 | <p> | |
82 | <b>Postconditions:</b> <code>r.get() == this</code>. | |
83 | </p> | |
84 | <p> | |
85 | <b>Throws:</b> <code>bad_weak_ptr</code> when no <code>shared_ptr</code> <em>owns</em> <code>*this</code>. | |
86 | </p> | |
87 | </blockquote> | |
88 | <h4><code>template<class T> weak_ptr<T> | |
89 | enable_shared_from_this<T>::weak_from_this() noexcept;</code></h4> | |
90 | <h4><code>template<class T> weak_ptr<T const> | |
91 | enable_shared_from_this<T>::weak_from_this() const noexcept;</code></h4> | |
92 | <blockquote> | |
93 | <p> | |
94 | <b>Requires:</b> <code>enable_shared_from_this<T></code> must be an | |
95 | accessible base class of <code>T</code>. <code>*this</code> must be a subobject | |
96 | of an instance <code>t</code> of type <code>T</code>. | |
97 | </p> | |
98 | <p> | |
99 | <b>Returns:</b> If a <code>shared_ptr</code> instance <code>p</code> that <em>owns</em> | |
100 | <code>t</code> exists or has existed in the past, a <code>weak_ptr<T></code> instance | |
101 | <code>r</code> that shares ownership with <code>p</code>. Otherwise, an empty <code>weak_ptr</code>. | |
102 | </p> | |
103 | </blockquote> | |
104 | <hr /> | |
105 | <p> | |
106 | <small>Copyright © 2002, 2003, 2015 by Peter Dimov. Distributed under the Boost Software License, Version | |
107 | 1.0. See accompanying file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or | |
108 | copy at <a href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>.</small></p> | |
109 | </body> | |
110 | </html> |