﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-firememory</title><link>http://www.cppblog.com/firememory/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 09 Jun 2026 17:39:11 GMT</lastBuildDate><pubDate>Tue, 09 Jun 2026 17:39:11 GMT</pubDate><ttl>60</ttl><item><title>enable_from_this方法的使用与陷阱(转)</title><link>http://www.cppblog.com/firememory/archive/2011/12/06/161577.html</link><dc:creator>firememory</dc:creator><author>firememory</author><pubDate>Tue, 06 Dec 2011 06:13:00 GMT</pubDate><guid>http://www.cppblog.com/firememory/archive/2011/12/06/161577.html</guid><wfw:comment>http://www.cppblog.com/firememory/comments/161577.html</wfw:comment><comments>http://www.cppblog.com/firememory/archive/2011/12/06/161577.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/firememory/comments/commentRss/161577.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/firememory/services/trackbacks/161577.html</trackback:ping><description><![CDATA[<div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; font-family: 宋体, Arial; font-size: 12px; background-color: #f5f7f8; "><span style="line-height: 1.5; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">原地址</span><a href="http://blog.chinaunix.net/space.php?uid=442138&amp;do=blog&amp;id=2122464">http://blog.chinaunix.net/space.php?uid=442138&amp;do=blog&amp;id=2122464</a><span style="line-height: 1.5; font-size: 13px; "><br /></span><span style="line-height: 1.5; font-size: 13px; "><br />ps:百度的不好用，我以后再也不用了，试验一下这个是否好用<br /><br />enable_from_this 的使用与实现原理说明：</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">shared_from_this()是enable_shared_from_this&lt;T&gt;的成员函数，返回shared_ptr&lt;T&gt;;</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">注意的是，这个函数仅在shared_ptr&lt;T&gt;的构造函数被调用之后才能使用。</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">原因是enable_shared_from_this::weak_ptr并不在构造函数中设置，而是在shared_ptr&lt;T&gt;的构造函数中设置。</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">错误的使用代码一：</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div></span></div><table border="1" cellspacing="0" cellpadding="0" bordercolor="#999999" width="95%" bgcolor="#F1F1F1" style="color: #000000; font-family: 宋体, Arial; font-size: 12px; line-height: 15px; background-color: #f5f7f8; border-collapse: collapse; "><tbody><tr><td style="line-height: 1.5; "><p style="margin-right: 5px; margin-left: 5px; padding-top: 5px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 21px; font-size: 14px; "><code><span style="line-height: 1.5; ">#<span style="line-height: 1.5; color: #ff0000; ">include</span>&nbsp;<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>boost/shared_ptr<span style="line-height: 1.5; color: #0000cc; ">.</span>hpp<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br />#<span style="line-height: 1.5; color: #ff0000; ">include</span>&nbsp;<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>boost/enable_shared_from_this<span style="line-height: 1.5; color: #0000cc; ">.</span>hpp<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br /><br />#<span style="line-height: 1.5; color: #ff0000; ">include</span>&nbsp;<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>iostream<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br />using namespace&nbsp;<span style="line-height: 1.5; color: #0000ff; ">std</span><span style="line-height: 1.5; color: #0000cc; ">;</span><br /><br />class D<span style="line-height: 1.5; color: #0000cc; ">:</span>&nbsp;<span style="line-height: 1.5; color: #0000ff; ">public</span>&nbsp;boost<span style="line-height: 1.5; color: #0000cc; ">:</span><span style="line-height: 1.5; color: #0000cc; ">:</span>enable_shared_from_this<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>D<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br />{<br /><span style="line-height: 1.5; color: #0000ff; ">public</span><span style="line-height: 1.5; color: #0000cc; ">:</span><br />&nbsp;&nbsp;&nbsp;&nbsp;D<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout<span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #ff00ff; ">"D::D()"</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span>endl<span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boost<span style="line-height: 1.5; color: #0000cc; ">:</span><span style="line-height: 1.5; color: #0000cc; ">:</span>shared_ptr<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>D<span style="line-height: 1.5; color: #0000cc; ">&gt;</span>&nbsp;p&nbsp;<span style="line-height: 1.5; color: #0000cc; ">=</span>&nbsp;shared_from_this<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br />}<span style="line-height: 1.5; color: #0000cc; ">;</span><br /><br /><span style="line-height: 1.5; color: #0000ff; ">int</span>&nbsp;main<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;boost<span style="line-height: 1.5; color: #0000cc; ">:</span><span style="line-height: 1.5; color: #0000cc; ">:</span>shared_ptr<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>D<span style="line-height: 1.5; color: #0000cc; ">&gt;</span>&nbsp;a<span style="line-height: 1.5; color: #0000cc; ">(</span>new D<span style="line-height: 1.5; color: #0000cc; ">)</span><span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;return 0<span style="line-height: 1.5; color: #0000cc; ">;</span>&nbsp;&nbsp;&nbsp;&nbsp;<br />}<br /></span></code></p></td></tr></tbody></table><br style="font-family: 宋体, Arial; font-size: 12px; line-height: 15px; background-color: #f5f7f8; " /><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; font-family: 宋体, Arial; font-size: 12px; background-color: #f5f7f8; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">程序编译通过，执行结果如下：</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">D::D()</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">terminate called after throwing an instance of 'boost::exception_detail::clone_impl&lt;boost::exception_detail::error_info_injector&lt;boost::bad_weak_ptr&gt; &gt;'</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">&nbsp;&nbsp;what(): &nbsp;tr1::bad_weak_ptr</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">Aborted</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">说明在D的构造函数中调用shared_from_this(), 此时D的实例本身尚未构造成功，weak_ptr也就尚未设置,所以程序抛出tr1::bad_weak_ptr异常。</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">错误的使用代码二：</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div></div><table border="1" cellspacing="0" cellpadding="0" bordercolor="#999999" width="95%" bgcolor="#F1F1F1" style="color: #000000; font-family: 宋体, Arial; font-size: 12px; line-height: 15px; background-color: #f5f7f8; border-collapse: collapse; "><tbody><tr><td style="line-height: 1.5; "><p style="margin-right: 5px; margin-left: 5px; padding-top: 5px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 21px; font-size: 14px; "><code><span style="line-height: 1.5; ">#<span style="line-height: 1.5; color: #ff0000; ">include</span>&nbsp;<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>boost/shared_ptr<span style="line-height: 1.5; color: #0000cc; ">.</span>hpp<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br />#<span style="line-height: 1.5; color: #ff0000; ">include</span>&nbsp;<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>boost/enable_shared_from_this<span style="line-height: 1.5; color: #0000cc; ">.</span>hpp<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br /><br />#<span style="line-height: 1.5; color: #ff0000; ">include</span>&nbsp;<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>iostream<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br />using namespace&nbsp;<span style="line-height: 1.5; color: #0000ff; ">std</span><span style="line-height: 1.5; color: #0000cc; ">;</span><br /><br />class D<span style="line-height: 1.5; color: #0000cc; ">:</span>&nbsp;<span style="line-height: 1.5; color: #0000ff; ">public</span>&nbsp;boost<span style="line-height: 1.5; color: #0000cc; ">:</span><span style="line-height: 1.5; color: #0000cc; ">:</span>enable_shared_from_this<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>D<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br />{<br /><span style="line-height: 1.5; color: #0000ff; ">public</span><span style="line-height: 1.5; color: #0000cc; ">:</span><br />&nbsp;&nbsp;&nbsp;&nbsp;D<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout<span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #ff00ff; ">"D::D()"</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span>endl<span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;void func<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout<span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #ff00ff; ">"D::func()"</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span>endl<span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boost<span style="line-height: 1.5; color: #0000cc; ">:</span><span style="line-height: 1.5; color: #0000cc; ">:</span>shared_ptr<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>D<span style="line-height: 1.5; color: #0000cc; ">&gt;</span>&nbsp;p&nbsp;<span style="line-height: 1.5; color: #0000cc; ">=</span>&nbsp;shared_from_this<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br />}<span style="line-height: 1.5; color: #0000cc; ">;</span><br /><br /><span style="line-height: 1.5; color: #0000ff; ">int</span>&nbsp;main<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;D d<span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;d<span style="line-height: 1.5; color: #0000cc; ">.</span>func<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;return 0<span style="line-height: 1.5; color: #0000cc; ">;</span>&nbsp;&nbsp;&nbsp;&nbsp;<br />}</span></code></p></td></tr></tbody></table><br style="font-family: 宋体, Arial; font-size: 12px; line-height: 15px; background-color: #f5f7f8; " /><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; font-family: 宋体, Arial; font-size: 12px; background-color: #f5f7f8; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">程序编译通过，执行结果如下：</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">D::D()</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">D::func()</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">terminate called after throwing an instance of 'boost::exception_detail::clone_impl&lt;boost::exception_detail::error_info_injector&lt;boost::bad_weak_ptr&gt; &gt;'</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">&nbsp;&nbsp;what(): &nbsp;tr1::bad_weak_ptr</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">Aborted</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">失败原因分析：</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">在主函数main中，D的实例是在栈上构造，没有使用boost::shared_ptr&lt;D&gt; 的构造方式，</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">所以boost::enable_shared_from_this&lt;D&gt;中的weak_ptr所指的函数对象也就没有被赋值，</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">在调用d.func()中使用shared_from_this()函数时</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">----注：shared_from_this的函数实现 ------</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">&nbsp;&nbsp; &nbsp;shared_ptr&lt;T&gt; shared_from_this()</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">&nbsp;&nbsp; &nbsp;{</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;shared_ptr&lt;T&gt; p( weak_this_ );</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;BOOST_ASSERT( p.get() == this );</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;return p;</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">&nbsp;&nbsp; &nbsp;}</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">----注：shared_from_this的函数实现 ------</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">调用BOOST_ASSERT( p.get() == this ); &nbsp;失败，抛出以上异常。</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">最后，我们给出share_from_this()的正确使用例子:</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "></div></div><table border="1" cellspacing="0" cellpadding="0" bordercolor="#999999" width="95%" bgcolor="#F1F1F1" style="color: #000000; font-family: 宋体, Arial; font-size: 12px; line-height: 15px; background-color: #f5f7f8; border-collapse: collapse; "><tbody><tr><td style="line-height: 1.5; "><p style="margin-right: 5px; margin-left: 5px; padding-top: 5px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 21px; font-size: 14px; "><code><span style="line-height: 1.5; ">#<span style="line-height: 1.5; color: #ff0000; ">include</span>&nbsp;<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>boost/shared_ptr<span style="line-height: 1.5; color: #0000cc; ">.</span>hpp<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br />#<span style="line-height: 1.5; color: #ff0000; ">include</span>&nbsp;<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>boost/enable_shared_from_this<span style="line-height: 1.5; color: #0000cc; ">.</span>hpp<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br /><br />#<span style="line-height: 1.5; color: #ff0000; ">include</span>&nbsp;<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>iostream<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br />using namespace&nbsp;<span style="line-height: 1.5; color: #0000ff; ">std</span><span style="line-height: 1.5; color: #0000cc; ">;</span><br /><br />class D<span style="line-height: 1.5; color: #0000cc; ">:</span>&nbsp;<span style="line-height: 1.5; color: #0000ff; ">public</span>&nbsp;boost<span style="line-height: 1.5; color: #0000cc; ">:</span><span style="line-height: 1.5; color: #0000cc; ">:</span>enable_shared_from_this<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>D<span style="line-height: 1.5; color: #0000cc; ">&gt;</span><br />{<br /><span style="line-height: 1.5; color: #0000ff; ">public</span><span style="line-height: 1.5; color: #0000cc; ">:</span><br />&nbsp;&nbsp;&nbsp;&nbsp;D<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout<span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #ff00ff; ">"D::D()"</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span>endl<span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;void func<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout<span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #ff00ff; ">"D::func()"</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span><span style="line-height: 1.5; color: #0000cc; ">&lt;</span>endl<span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;boost<span style="line-height: 1.5; color: #0000cc; ">:</span><span style="line-height: 1.5; color: #0000cc; ">:</span>shared_ptr<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>D<span style="line-height: 1.5; color: #0000cc; ">&gt;</span>&nbsp;p&nbsp;<span style="line-height: 1.5; color: #0000cc; ">=</span>&nbsp;shared_from_this<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br />}<span style="line-height: 1.5; color: #0000cc; ">;</span><br /><br /><span style="line-height: 1.5; color: #0000ff; ">int</span>&nbsp;main<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;boost<span style="line-height: 1.5; color: #0000cc; ">:</span><span style="line-height: 1.5; color: #0000cc; ">:</span>shared_ptr<span style="line-height: 1.5; color: #0000cc; ">&lt;</span>D<span style="line-height: 1.5; color: #0000cc; ">&gt;</span>&nbsp;p<span style="line-height: 1.5; color: #0000cc; ">(</span>new D<span style="line-height: 1.5; color: #0000cc; ">)</span><span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;p<span style="line-height: 1.5; color: #0000cc; ">-</span><span style="line-height: 1.5; color: #0000cc; ">&gt;</span>func<span style="line-height: 1.5; color: #0000cc; ">(</span><span style="line-height: 1.5; color: #0000cc; ">)</span><span style="line-height: 1.5; color: #0000cc; ">;</span><br />&nbsp;&nbsp;&nbsp;&nbsp;return 0<span style="line-height: 1.5; color: #0000cc; ">;</span>&nbsp;&nbsp;&nbsp;&nbsp;<br />}&nbsp;&nbsp;&nbsp;&nbsp;</span></code></p></td></tr></tbody></table><br style="font-family: 宋体, Arial; font-size: 12px; line-height: 15px; background-color: #f5f7f8; " /><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; font-family: 宋体, Arial; font-size: 12px; background-color: #f5f7f8; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">执行结果:</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">D::D()</span></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: 1.5; "><span style="line-height: 1.5; font-size: 13px; ">D::func()</span><span style="line-height: 1.5; white-space: pre; "><span style="line-height: 1.5; font-size: 13px; ">	</span></span></div></div></div><img src ="http://www.cppblog.com/firememory/aggbug/161577.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/firememory/" target="_blank">firememory</a> 2011-12-06 14:13 <a href="http://www.cppblog.com/firememory/archive/2011/12/06/161577.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>