shared_ptr给管理的对象定制析沟函数

示例代码

#include <iostream>
#include <memory>

using namespace std;

class base
{
public:
        ~base(){cout << "in ~base()" << endl;};
};

void fuc(base* pb)
{
        cout << "in fuc()" << endl;
        delete pb; //此处必须显示的释放pb
}

int main()
{
        shared_ptr<base> ptr(new base, fuc); //new base返回的指针会传递给fuc
        ptr.reset();//使用fuc函数释放ptr所管理的对象
        while(1);
        return 0;
}

运行结果

[lhx@lhx-master share_ptr]$ ./a.out 
in fuc()
in ~base()

原文地址:https://www.cnblogs.com/DXGG-Bond/p/14684895.html