#include <boost/scope_exit.hpp>
#include <iostream>
int *foo()
{
int *i = new int{10};
BOOST_SCOPE_EXIT(&i)
{
delete i;
i = 0;
} BOOST_SCOPE_EXIT_END
std::cout << *i << '\n';
return i;
}
int main()
{
int *j = foo();
std::cout << j << '\n';
}
Boost scope_exit用于替换RAII
最新推荐文章于 2023-09-04 00:36:31 发布
本文介绍了一个C++示例程序,展示了如何利用Boost.Scope_exit来确保资源(如动态分配的内存)得到正确释放。这种方法能有效防止资源泄露,并且能够使代码更加健壮。
4607

被折叠的 条评论
为什么被折叠?



