C++ 智能指针学习 auto_ptr, unique_ptr 。。。

来源:互联网 发布:李炎恢php 编辑:程序博客网 时间:2024/06/16 14:38

std::auto_ptr

std::auto_ptr的认识和使用

C++智能指针(auto_ptr)详解
http://blog.sina.com.cn/s/blog_7708265a01010lyv.html

http://blog.csdn.net/gui694278452/article/details/46348733

内存问题 与 智能指针(一)

http://blog.csdn.net/qq_26437925/article/details/53738165

std::auto_ptr 使用的一些注意和限制

class Simple {public:    Simple(int param = 0) {        number = param;        std::cout << "Simple: " << number << std::endl;     }    ~Simple() {        std::cout << "~Simple: " << number << std::endl;    }    void PrintSomething() {        std::cout << "PrintSomething: " << info_extend.c_str() << std::endl;    }public:    std::string info_extend;    int number;};void TestAutoPtr2() {    std::auto_ptr<Simple> my_memory(new Simple(1));    if (my_memory.get()) {        std::auto_ptr<Simple> my_memory2; // 创建一个新的 my_memory2 对象        my_memory2 = my_memory; // 复制旧的 my_memory 给 my_memory2(my_memory2拥有所有权,my_memory不再有效)        my_memory2->PrintSomething(); // 输出信息,复制成功        my_memory->PrintSomething(); // 崩溃    }}void TestAutoPtr3(){    std::auto_ptr<Simple> my_memory(new Simple(1));    if (my_memory.get()) {        my_memory.release(); // 让出所有权,并非释放内存    }}/** 按值传递时,函数调用过程中在函数的作用域中会产生一个局部对象来接收传入的auto_ptr(拷贝构造),* 这样,传入的实参auto_ptr就失去了其对原对象的所有权,* 而该对象会在函数退出时被局部auto_ptr删除*/void f(auto_ptr<int> ap) { cout << *ap << endl; }void TestAutoPtr4(){    auto_ptr<int> ap1(new int(0));    f(ap1);    cout<<*ap1 << endl;}

std::auto_ptr不可用于数组

void TestAutoPtr5(){    vector<std::auto_ptr<int>> v;    auto_ptr<int> ap1(new int(3));    auto_ptr<int> ap2(new int(1));    auto_ptr<int> ap3(new int(2));    v.push_back(ap1);    v.push_back(ap2);    v.push_back(ap3);}

这里写图片描述

总结下有如下的几个:

  • 尽量不要使用“operator=”。如果使用了,请不要再使用先前对象。

  • release() 函数不会释放对象,仅仅归还所有权。

  • std::auto_ptr 最好不要当成参数传递。

  • 由于 std::auto_ptr 的“operator=”问题,有其管理的对象不能放入 std::vector 等容器中。

  • std::auto_ptr不能用于数组(转移指针资源的所有权的同时将原指针置为NULL,这跟通常理解的copy行为是不一致的)

  • 。。。


参考

auto_ptr的使用问题分析

http://www.cnblogs.com/tekkaman/archive/2013/03/09/2951521.html

C++11智能指针之unique_ptr

auto_ptr 与 unique_ptr的区别

  • auto_ptr有拷贝语义,拷贝后源对象变得无效;unique_ptr则无拷贝语义,但提供了移动语义

  • auto_ptr不可作为容器元素,unique_ptr可以作为容器元素

  • auto_ptr不可指向动态数组(尽管不会报错,但不会表现出正确行为),unique_ptr可以指向动态数组

std::unique_ptr 可移动,但不可复制;“移动”将所有权转移到新 unique_ptr 并重置旧 unique_ptr

unique_ptr<int> ap(new int(88));unique_ptr<int> one (ap) ; // 会出错unique_ptr<int> two = one; //会出错
vector<std::unique_ptr<int>> v;unique_ptr<int> ap1(new int(3));unique_ptr<int> ap2(new int(1));unique_ptr<int> ap3(new int(2));v.push_back(std::move(ap1)); //这里需要显式的移动语义,因为unique_ptr并无copy语义v.push_back(std::move(ap2));v.push_back(std::move(ap3));for(int i=0;i<3;i++) {    cout << *v[i] << endl;}

基础用法

//智能指针的创建  unique_ptr<int> u_i;u_i.reset(new int(3)); //"绑定”动态对象  cout << *u_i.get() << endl;unique_ptr<int> u_i2(new int(4));//创建时指定动态对象  //所有权的变化int *p_i = u_i2.release(); //释放所有权  if(u_i2.get() == nullptr){    cout << "nullptr" << endl;    u_i2 = nullptr;}cout << *p_i << endl;unique_ptr<string> u_s(new string("abc"));  unique_ptr<string> u_s2 = std::move(u_s); //所有权转移(通过移动语义),u_s所有权转移后,变成“空指针”  if(u_s.get() == nullptr){    cout << "nullptr" << endl;}u_s2 = nullptr;//显式销毁所指对象,同时智能指针变为空指针。与u_s2.reset()等价  

0 0
原创粉丝点击