﻿<?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++博客-aa19870406-随笔分类-STL/Boost</title><link>http://www.cppblog.com/aa19870406/category/16150.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 02 Jun 2011 12:14:04 GMT</lastBuildDate><pubDate>Thu, 02 Jun 2011 12:14:04 GMT</pubDate><ttl>60</ttl><item><title>容器中对象为指针时的注意事项</title><link>http://www.cppblog.com/aa19870406/archive/2011/05/31/147780.html</link><dc:creator>MrRightLeft</dc:creator><author>MrRightLeft</author><pubDate>Tue, 31 May 2011 12:18:00 GMT</pubDate><guid>http://www.cppblog.com/aa19870406/archive/2011/05/31/147780.html</guid><wfw:comment>http://www.cppblog.com/aa19870406/comments/147780.html</wfw:comment><comments>http://www.cppblog.com/aa19870406/archive/2011/05/31/147780.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/aa19870406/comments/commentRss/147780.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/aa19870406/services/trackbacks/147780.html</trackback:ping><description><![CDATA[1.容器在销毁时，会自动帮你销毁容器中所存储的额对象。<br />2.当容器中对象是指针的时候，容器销毁的时候，也会帮你销毁容器中的对象-指针。<br />3.由于容器销毁销毁的只是指针值，而非指针所指对象。则会出现资源泄露<br />解决方法：<br />1.用智能指针auto_ptr,shared_ptr来代替指针。思想：用类对象来管理指针的创建销毁。构造函数创建指针，析构函数销毁指针所指对象。<br />2.显示销毁指针所指对象，使用for_each，结合函数对象。可以实现异常安全<br /><span style="color: red; ">注：刚看了&lt;&lt;effective stl&gt;&gt;条款8，里面说如果使用auto_ptr作为容器对象是愚蠢的行为。所以上面的方法1，用shared_ptr而不能用auto_ptr。<br />&nbsp;&nbsp; &nbsp; &nbsp;主要是因为auto_ptr在拷贝的时候有一个所有权的转移。</span><img src ="http://www.cppblog.com/aa19870406/aggbug/147780.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/aa19870406/" target="_blank">MrRightLeft</a> 2011-05-31 20:18 <a href="http://www.cppblog.com/aa19870406/archive/2011/05/31/147780.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>