c++ primer 12.1.4节练习(自定义 shared_ptr的删除器)

来源:互联网 发布:ubuntu没有vim命令 编辑:程序博客网 时间:2024/06/05 17:36

12.14

#include <iostream>  #include <string>#include <vector>#include <memory>using namespace std;string destination = "目的";string connection = "连接";string Connect(string *) //获得一个连接{cout << "获得一个连接" << endl;string temp = "连接1";return temp;}void disconnect(string test)//关闭一个连接{test.clear();cout << "关闭连接" << endl;}void end_connect(string *p)//结束一个连接{disconnect(*p);}void f(string &d){string c = Connect(&d);cout << c << endl;shared_ptr<string>p(&c, end_connect);}int main(){f(connection);//即,首先connection进入Connect,c被置为“连接1”  //退出f之后c被销毁,而connection也被end_connect给清空system("pause");return 0;}


12.15

更新中...............

原创粉丝点击