c++智能指针:boost::scoped_ptr,boost::shared_ptr,boost::scoped_array
来源:互联网 发布:美工课教案 编辑:程序博客网 时间:2024/05/29 05:56
boost::scoped_ptr使用示例:
#include <iostream>#include <string>#include <boost/smart_ptr.hpp>using namespace std;class TestBody{public: TestBody(int param = 0):m_num(param) { m_info = "INFO"; cout<<"construct TestBody"<<endl; } ~TestBody() { cout<<"destory TestBody"<<endl; } void SetInfo(string info) { m_info = info; } void PrintInfo() { cout<<"info:"<<m_info<<endl; }private: int m_num; string m_info;};void TestBoostScopedPtr(){ boost::scoped_ptr<TestBody> smartPoint1(new TestBody(1)); if(smartPoint1.get()) { smartPoint1->PrintInfo(); smartPoint1->SetInfo("TestBoostScopedPtr"); smartPoint1->PrintInfo(); }}int main(void){ TestBoostScopedPtr(); return 0;}
运行结果:
如果增加赋值语句,则会提示编译错误。
void TestBoostScopedPtr(){ boost::scoped_ptr<TestBody> smartPoint1(new TestBody(1)); if(smartPoint1.get()) { smartPoint1->PrintInfo(); smartPoint1->SetInfo("TestBoostScopedPtr"); smartPoint1->PrintInfo(); boost::scoped_ptr<TestBody> smartPoint2; smartPoint2 = smartPoint1; //编译错误 }}
查看源码实现,发现赋值语句声明为private
template<class T> class scoped_ptr // noncopyable{private: T * px; scoped_ptr(scoped_ptr const &); scoped_ptr & operator=(scoped_ptr const &); typedef scoped_ptr<T> this_type; void operator==( scoped_ptr const& ) const; void operator!=( scoped_ptr const& ) const;public:}
boost::shared_ptr使用示例:
void TestBoostSharedPtr(){ boost::shared_ptr<TestBody> smartPoint1(new TestBody(1)); if(smartPoint1.get()) { smartPoint1->PrintInfo(); smartPoint1->SetInfo("TestBoostSharedPtr"); cout<<"use_count Point1:"<<smartPoint1.use_count()<<endl; boost::shared_ptr<TestBody> smartPoint2; smartPoint2 = smartPoint1; smartPoint2->PrintInfo(); smartPoint1->PrintInfo(); cout<<"use_count Point1:"<<smartPoint1.use_count()<<endl; cout<<"use_count Point2:"<<smartPoint2.use_count()<<endl; } cout<<"use_count Point1:"<<smartPoint1.use_count()<<endl;}
执行结果:
boost::scoped_array使用示例:
void TestBoostScopedArray(){ boost::scoped_array<TestBody> smartPoint(new TestBody[3]); if(smartPoint.get()) { smartPoint[0].PrintInfo(); smartPoint[1].PrintInfo(); smartPoint[2].PrintInfo(); smartPoint[0].SetInfo("TestBoostSharedPtr0"); smartPoint[1].SetInfo("TestBoostSharedPtr1"); smartPoint[0].PrintInfo(); smartPoint[1].PrintInfo(); }}
运行结果:
和scoped_ptr类似,赋值运算符被声明为私有。 ->改为用。标识。
0 0
- c++智能指针:boost::scoped_ptr,boost::shared_ptr,boost::scoped_array
- boost 智能指针auto_ptr scoped_ptr shared_ptr scoped_array shared_array 总结
- 智能指针 boost(scoped_ptr,scoped_array,shared_ptr,shared_array) 和 std (auto_ptr)的比较 .
- Boost智能指针:scoped_ptr shared_ptr weak_ptr
- 浅析Boost智能指针:scoped_ptr shared_ptr weak_ptr
- 智能指针boost::scoped_ptr
- Boost智能指针:scoped_ptr
- boost::scoped_ptr智能指针
- boost.smart_ptr-智能指针scoped_array
- boost.smart_ptr-智能指针scoped_array
- Boost智能指针:shared_ptr
- 智能指针boost::shared_ptr
- 智能指针boost::shared_ptr
- Boost智能指针:shared_ptr
- C++:Boost智能指针——scoped_ptr
- 几种智能指针的比较(std::auto_ptr、boost::scoped_ptr、boost::shared_ptr、boost::weak_ptr)
- Boost智能指针——scoped_ptr和shared_ptr
- Boost智能指针——scoped_ptr、shared_ptr、weak_ptr、intrusive_ptr
- 积跬步,聚小流------关闭iphone浏览器自动识别数字为电话号码
- 详细解析mtk android5.1(mt6580/mt8321)的display system
- 关于servlet开发时,web.xml的配置问题。
- 秒杀多线程第五篇 经典线程同步 关键段CS
- Android性能中容易忽视的优化点有哪些?
- c++智能指针:boost::scoped_ptr,boost::shared_ptr,boost::scoped_array
- 秒杀多线程第六篇 经典线程同步 事件Event
- JAVA关于HashMap浅谈
- 将apache添加至windows服务
- springAOP事物管理不生效原因总结
- SVN:版本库管理
- Java 的系统属性 sun.jnu.encoding 和 file.encoding 的区别
- $.grep函数讨论
- Effective Modern C++ Item1 模板类型推导详解