forked from boostorg/smart_ptr
86 lines
3.3 KiB
HTML
86 lines
3.3 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<title>enable_shared_from_this</title>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
</head>
|
|
<body text="#000000" bgcolor="#ffffff" link="#0000ff" vlink="#0000ff">
|
|
<h1><img height="86" alt="boost.png (6897 bytes)" src="../../boost.png"
|
|
width="277" align="middle" border="0">enable_shared_from_this</h1>
|
|
<h2><a name="Purpose">Purpose</a></h2>
|
|
<p>
|
|
The header <STRONG><boost/enable_shared_from_this.hpp></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<T></STRONG> defines two member functions
|
|
called <STRONG>shared_from_this</STRONG> that return a <STRONG>shared_ptr<T></STRONG>
|
|
and <STRONG>shared_ptr<T const></STRONG>, depending on constness, to <STRONG>this</STRONG>.</P>
|
|
<h2><a name="Example">Example</a></h2>
|
|
<pre>
|
|
#include <boost/enable_shared_from_this.hpp>
|
|
#include <boost/shared_ptr.hpp>
|
|
#include <cassert>
|
|
|
|
class Y: public boost::enable_shared_from_this<Y>
|
|
{
|
|
public:
|
|
|
|
boost::shared_ptr<Y> f()
|
|
{
|
|
return shared_from_this();
|
|
}
|
|
};
|
|
|
|
int main()
|
|
{
|
|
boost::shared_ptr<Y> p(new Y);
|
|
boost::shared_ptr<Y> q = p->f();
|
|
assert(p == q);
|
|
assert(!(p < q || q < p)); // p and q must share ownership
|
|
}
|
|
</pre>
|
|
<h3><a name="Synopsis">Synopsis</a></h3>
|
|
<pre>
|
|
namespace boost
|
|
{
|
|
|
|
template<class T> class enable_shared_from_this
|
|
{
|
|
public:
|
|
|
|
shared_ptr<T> shared_from_this();
|
|
shared_ptr<T const> shared_from_this() const;
|
|
}
|
|
|
|
}
|
|
</pre>
|
|
<h4>template<class T> shared_ptr<T>
|
|
enable_shared_from_this<T>::shared_from_this();</h4>
|
|
<h4>template<class T> shared_ptr<T const>
|
|
enable_shared_from_this<T>::shared_from_this() const;</h4>
|
|
<blockquote>
|
|
<p>
|
|
<b>Requires:</b> <STRONG>enable_shared_from_this<T></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<T></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>$Date$</p>
|
|
<p>
|
|
<small>Copyright © 2002, 2003 by Peter Dimov. Distributed under the Boost Software License, Version
|
|
1.0. See accompanying file <A href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or
|
|
copy at <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>.</small></p>
|
|
</body>
|
|
</html>
|