c/c++ 智能指针 unique_ptr 使用

智能指针 unique_ptr 使用

和shared_ptr不同,可以有多个shared_ptr指向同一个内存,只能有1个unique_ptr指向某个内存。因此unique_ptr不支持普通的拷贝和赋值。

一,先来个表格,唠唠unique_ptr

操作 功能描述
unique_ptr<T> u(q) 智能指针u管理内置指针q所指向的对象;q必须指向new分配的内存,且能够转换为T*。
unique_ptr<T, D> u(u, d) 用类型为D的对象d来代替delete
u = nullptr 释放u指向的对象,并将u置为空
u.release() u放弃对指针的控制权,返回内置指针,并将u置为空
u.reset() 释放u所指向的对象,并将u置为空。
u.reset(q) 如果还传递了参数q,让u指向q
u.reset(q, d) 如果还传递了参数d,将会调用d,而不是delete来释放q

小例子索引

代码块 功能描述
test1 不可以拷贝和赋值
test2 自定义删除器
test3 reset和release的使用
test4 unique_ptr作为函数的返回值

小例子

include <iostream>
#include <memory>
#include <vector>

using namespace std;

class Test{
public:
  Test(int d = 0) : data(d){cout << "new" << data << endl;}
  ~Test(){cout << "del" << data << endl;}
private:
  int data;
};
void my_deleter(Test* t){
  cout << "worked" << endl;
}
unique_ptr<int> cl1(int p){
  return unique_ptr<int>(new int(p));
}
unique_ptr<int> cl2(int p){
  unique_ptr<int> rt(new int(p));
  return rt;
}
void fl1(unique_ptr<int> p){
  *p = 100;
}

int main(){
  //test1 不可以拷贝和赋值                                                      
  /*                                                                            
  unique_ptr<int> p1(new int(11));                                              
  //unique_ptr<int> p2(p1);//NG                                                 
  unique_ptr<int> p3(new int(10));                                              
  //p3 = p1;//NG                                                                
  */

  //test2 自定义删除器                                                          
  //不再调用Test的析构函数了                                                    
  //unique_ptr<Test, decltype(my_deleter)*> u(new Test(1), my_deleter);         

  //test3 reset和release的使用
  /*                                                                            
  unique_ptr<Test> p1(new Test(1));                                             
  unique_ptr<Test> p2(p1.release());//将p1置为空,p2指向了原来p1指向的对象 
  unique_ptr<Test> p3(new Test(3));                                             
  p2.reset(p3.release());//先释放了p2所指向的内存,让p2指向了原来p3指向的对象,p3被置为空
  p2.release();//错误,p2不会释放内存,而且丢失了能够释放内存的指针
  auto p = p2.release();//正确,但必须要记得delete(p)                           
  */

  //test4 unique_ptr作为函数的返回值                                            
  /*                                                                            
  unique_ptr<int> p1 = cl1(11);                                                 
  cout << *p1 << endl;                                                          
  unique_ptr<int> p2 = cl2(22);                                                 
  cout << *p2 << endl;                                                          
  //fl1(p2);//NG 编译不过                                                       
  */
}

github完整代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

猜你喜欢

转载自www.cnblogs.com/xiaoshiwang/p/9716650.html