shared_ptr做资源删除器

struct dialog_t 

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


template
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(new dialog_t, deleter_t{});
p->fun();
}
getchar();
return 0;

}


利用shared_ptr管理资源


高级用法

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


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


离开作用域调用 any_func。

你可能感兴趣的:(C++)