智能指针
2016-07-22 22:08
309 查看
智能指针
智能指针stdauto_ptr
boostscoped_ptr
boostshared_ptr
boostscoped_array
boostshared_array
boostweak_ptr
boostinstrusive_ptr
后记
std::auto_ptr
使用总结1.(deprecated since C++11) (until C++17)1
2.尽量不要使用”=”. 如果使用了, 请不要再使用先前对象.
3.记住release(0)函数不会释放对象, 仅仅归还所有权, release(0)返回T*类型.
4.由于std::auto_ptr的”operator=”问题, 由其管理的对象不能放入 std::vector 等容器中. 同样的道理, std::auto_ptr最好不要当成参数传递.
示例代码
#include "simple.h" #include <memory> int main() { std::auto_ptr<Simple> my_memory(new Simple(1)); // 创建对象,输出:Simple:1 if (my_memory.get()) { // 判断智能指针是否为空 my_memory->PrintSomething(); // 使用 operator-> 调用智能指针对象中的函数 my_memory.get()->info_extend = "Addition"; // 使用 get() 返回裸指针,然后给内部对象赋值 my_memory->PrintSomething(); // 再次打印,表明上述赋值成功 (*my_memory).info_extend += " other"; // 使用 operator* 返回智能指针内部对象,然后用“.”调用智能指针对象中的 函数 my_memory->PrintSomething(); // 再次打印,表明上述赋值成功 } std::auto_ptr<Simple> instance(new Simple(1)); if(instance.get()){ std::auto_ptr<Simple> instance2; instance2 = instance; //导致instance悬空; instance2->PrintSomething(); //instance->PrintSomething(); //崩溃; //instance2->release(); //并未释放内存, 只交出使用权; release返回Simple*; //instance2->reset(); //释放内存; reset(1)已经被启用; //delete instance2->release(); 也可释放内存; 但是release(0)已经被弃用; }
boost::scoped_ptr
总结1.独享内存. 即不能被赋值给另一个同类scoped_ptr指针.
2.因为具有1所描述的特性, 避免了std::auto_ptr的几个问题. 比如赋值会编译错误;
3.没有release(0), 可以通过reset(1)释放内存.
代码
:
#include "simple.h" #include <boost/smart_ptr.hpp> int main() { boost::scoped_ptr<Simple> my_memory(new Simple(1)); // 创建对象,输出:Simple:1 if (my_memory.get()) { // 判断智能指针是否为空 my_memory->PrintSomething(); // 使用 operator-> 调用智能指针对象中的函数 my_memory.get()->info_extend = "Addition"; // 使用 get() 返回裸指针,然后给内部对象赋值 my_memory->PrintSomething(); // 再次打印,表明上述赋值成功 (*my_memory).info_extend += " other"; // 使用 operator* 返回智能指针内部对象,然后用“.”调用智能指针对象中的函数 my_memory->PrintSomething(); // 再次打印,表明上述赋值成功 } boost::scoped_ptr<Simple> instance(new Simple(1)); if(instance.get()){ boost::scoped_ptr<Simple> instance2; //instance2 = instance; //err, forbidden by compiler; //instance->release(); //err, has no such member function; } return 0; }
boost::shared_ptr
总结1.共享内存, 具有operator=操作, 带引用计数.
代码
#include "simple.h" #include <boost/smart_ptr.hpp> void TestSharedPtr(boost::shared_ptr<Simple> memory) { // 注意:无需使用 reference (或 const reference) memory->PrintSomething(); std::cout << "TestSharedPtr UseCount: " << memory.use_count() << std::endl; } int main() { boost::shared_ptr<Simple> my_memory(new Simple(1)); // 创建对象,输出:Simple:1 if (my_memory.get()) { // 判断智能指针是否为空 my_memory->PrintSomething(); // 使用 operator-> 调用智能指针对象中的函数 my_memory.get()->info_extend = "Addition"; // 使用 get() 返回裸指针,然后给内部对象赋值 my_memory->PrintSomething(); // 再次打印,表明上述赋值成功 (*my_memory).info_extend += " other"; // 使用 operator* 返回智能指针内部对象,然后用“.”调用智能指针对象中的函数 my_memory->PrintSomething(); // 再次打印,表明上述赋值成功 } std::cout << "TestSharedPtr2 UseCount: " << my_memory.use_count() << std::endl; TestSharedPtr(my_memory); std::cout << "TestSharedPtr2 UseCount: " << my_memory.use_count() << std::endl; //my_memory.release();// 编译 error: 同样,shared_ptr 也没有 release 函数 return 0; }
boost::scoped_array
scoped_ptr的数组版代码
:
#include "simple.h" #include <boost/smart_ptr.hpp> int main(){ boost::scoped_array<Simple> test_array(new Simple[2]); if(test_array.get()){ test_array[0].PrintSomething(); test_array.get()[0].info_extend = "Addition"; test_array[0].PrintSomething(); //(*test_array)[0].info_extend += "other"; //err, scoped_array has no *; //test_array[0].release(); //test_array[0] is a Simple; boost::scoped_array<Simple> test_array2; //test_array2=test_array; //err scoped_array has no operator=; } }
boost::shared_array
shared_ptr的数组版代码
#include "simple.h" #include <boost/smart_ptr.hpp> void test_shared_array(boost::shared_array<Simple> memory){ // no need to be reference or const reference; std::cout << "Test shared array use count:" << memory.use_count() << std::endl; } int main(){ boost::shared_array<Simple> test_array(new Simple[2]); if(test_array.get()){ test_array[0].PrintSomething(); test_array.get()[0].info_extend = "Addition"; test_array[0].PrintSomething(); //(*test_array)[0].info_extend += "other"; //err, shared_array has no *; //test_array[0].release(); //test_array[0] is a Simple; boost::shared_array<Simple> test_array2; test_array2=test_array; std::cout << "Test shared array use count:" << test_array.use_count() << std::endl; } std::cout << "Test shared array use count:" << test_array.use_count() << std::endl; test_shared_array(test_array); std::cout << "Test shared array use count:" << test_array.use_count() << std::endl; }
boost::weak_ptr
boost::instrusive_ptr
后记
目前这些都只是常识, 关于智能指针还有很多需要注意的地方, 和很多可以巧妙运用的地方. 限于现在水平有限, 后面再补充.重要参考博文:2
相关文章推荐
- The C++ Standard Library: shared_ptr and unique_ptr
- C++11 智能指针——shared_ptr
- effective c++ 以对象管理资源
- effective c++ 在资源管理类中小心copying 行为
- effective c++在资源管理类中提供对原始资源的访问
- effective c++ 避免返回handles指向对象内部成分(1)
- effective c++避免返回handles指向对象内部成分(2)
- 我所熟悉的C++智能指针auto_ptr vs shared_ptr (二)
- C++四种智能指针小结
- C++11 智能指针之 std::shared_ptr 初级学习
- 动态内存与智能指针
- 自己实现 SharedPtr(4)—— 一个代码洁癖的救赎
- C++智能指针 shared_ptr,unique_ptr和weak_ptr
- 动态内存管理和智能指针 2.0 -- shared_ptr
- c++ enable shared from this
- C++智能指针shared_ptr剖析
- 智能指针应用
- shared_ptr智能指针源码剖析
- shared_ptr的理解和注意事项
- 【转载】信号量