64,637
社区成员
发帖
与我相关
我的任务
分享
try
{
A* pA = new A;//memory leak
throw 1;
}
catch(...)
{
}
A* pA;
try
{
pA = new A;//no memory leak
throw 1;
}
catch(...)
{
}
delete pA;
class File_handle {
FILE* p;
public:
File_handle(const char* n, const char* a)
{ p = fopen(n,a); if (p==0) throw Open_error(errno); }
File_handle(FILE* pp)
{ p = pp; if (p==0) throw Open_error(errno); }
~File_handle() { fclose(p); }
operator FILE*() { return p; }
// ...
};
void f(const char* fn)
{
File_handle f(fn,"rw"); // open fn for reading and writing
// use file through f
}