shared_ptr做资源删除器

来源:互联网 发布:王小波与李银河 知乎 编辑:程序博客网 时间:2024/06/09 14:05
struct dialog_t 

void fun(){
cout << "fun" << endl;
}
};


template<class T>
struct deleter_t
{
void operator () (T* t) const
{
if (t!= NULL)
{
cout << "delte" << endl;
delete t;
t = NULL;
}
}
};


int main()
{
{
auto const p = boost::shared_ptr<dialog_t>(new dialog_t, deleter_t<dialog_t>{});
p->fun();
}
getchar();
return 0;

}


利用shared_ptr管理资源


高级用法

void any_func(void *p)
{
cout << "some sth" << endl;
}


boost::shared_ptr<void> A_ptr((void*)0, any_func);


离开作用域调用 any_func。

0 0