它刚好发生在我身上,我想知道在下列情况下如何释放资源.
class Base {
Resource *r;
public:
Base() { /* ... */ }
~Base() {
delete r;
}
};
class Derived : public Base {
public:
Derived() { /* ... */ }
~Derived() {
/* Suddenly something here throws! */
}
};
int main() {
try {
Derived d;
} catch(...) {
/* what happened with Base::r !? */
}
}
Run Code Online (Sandbox Code Playgroud)
如果派生类析构函数抛出,是否会调用基类析构函数?或者会有泄漏吗?
如果对象的构造函数抛出异常,那么析构函数会被调用吗?或者这是未定义的行为?(这就是为什么我不愿意说出我的编译器的作用。)
struct foo()
{
foo(){
throw "bar";
}
~foo(){
/*am I called*/
}
};
foo f;
Run Code Online (Sandbox Code Playgroud)