shared_ptr<int> p(new int(2)); f(p, g()); } // 下面这个就可能内存泄漏! void bad() { f(shared_ptr<int>(new int(2)), g()); } 看看基本的使用例子。 #include <boost/smart_ptr.hpp> #include <iostream> int main() { // Basic useage boost::shared_ptr<int> p1(new int(10...
boost的smart_ptr中提供了4种智能指针和2种智能指针数组来作为std::auto_ptr的补充。 shared_ptr<boost/shared_ptr.hpp>:使用shared_ptr进行对象的生存期自动管理,使得分享资源所有权变得有效且安全. scoped_ptr<boost/scoped_ptr.hpp>: 用于确保能够正确地删除动态分配的对象。scoped_ptr 有着与std::auto_ptr类...
boost的smart_ptr中提供了4种智能指针和2种智能指针数组来作为std::auto_ptr的补充。 shared_ptr<boost/shared_ptr.hpp>:使用shared_ptr进行对象的生存期自动管理,使得分享资源所有权变得有效且安全. scoped_ptr<boost/scoped_ptr.hpp>: 用于确保能够正确地删除动态分配的对象。scoped_ptr 有着与std::auto_ptr类...
}boost::shared_ptr<string>sps(boost::make_shared<string>("shared_ptr_test")); cout <<"\n"<< sps->c_str() << endl;boost::shared_ptr<string>sps2(sps); *sps2 ="shared_ptr is smart!"; cout << *sps << endl;assert(sps == sps2);//shared_ptr用于容器typedefvector<boost::share...
下面就按照顺序讲解如上 7 种智能指针(smart_ptr)。 二、具体使用 1、总括 对于编译器来说,智能指针实际上是一个栈对象,并非指针类型,在栈对象生命期即将结束时,智能指针通过析构函数释放有它管理的堆内存。所有智能指针都重载了“operator->”操作符,直接返回对象的引用,用以操作对象。访问智能指针原来的方法则...
boost库实现了各种智能指针,基本上都纳入了c++11标准中,boost库的smart_ptr目录下就是各种指针的实现了: 1.shared_ptr template<class T> class shared_ptr { private: // Borland 5.5.1 specific workaround typedef shared_ptr<T> this_type; public: ...
boost::shared_ptr 主要特点 boost.smart_ptr 库中最有价值,最重要的组成部分。支持拷贝构造函数、支持赋值操作。重载了*和->操作符用来模仿原始指针的行为。目前已成为tr1标准的一部分,发展自原始的auto_ptr,内置引用计数。引用指针计数器记录有多少个引用指针指向同一个对象,如果最后一个引用指针被销毁的时候,...
(b) Smart Ptr库:智能指针 (c) Utility库:小工具的集合 解析库 (a)Spirit库:基于EBNF范式的LL解析器框架 编程接口库 (a)Function库:实现一个通用的回调机制,已被收入TR1 (b) Parameter库:提供使用参数名来指定函数参数的机制 综合类库 (a)Compressed Pair库:优化的对pair对象的存储 (b) CRC库:实现...
smart_ptr:5个智能指针,学习智能指针必读,一份不错的参考是来自CUJ的文章: Smart Pointers in Boost,哦,这篇文章可以查到,CUJ是提供在线浏览的。中文版见笔者在《Dr.Dobb's Journal软件研发杂志》第7辑上的译文。 Boost总体来说是实用价值很高,质量很高的库。并且由于其对跨平台的强调,对标准C++的强调,是编写...