您的位置:首页 > 其它

shared_ptr做资源删除器

2015-07-23 15:13 99 查看
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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: