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;
{
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。