相关疑难解决方法(0)

如果派生类析构函数抛出异常,基类析构函数会发生什么

它刚好发生在我身上,我想知道在下列情况下如何释放资源.

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)

如果派生类析构函数抛出,是否会调用基类析构函数?或者会有泄漏吗?

c++ destructor memory-leaks exception

23
推荐指数
1
解决办法
2117
查看次数

构造函数中抛出异常:析构函数是否被调用?

如果对象的构造函数抛出异常,那么析构函数会被调用吗?或者这是未定义的行为?(这就是为什么我不愿意说出我的编译器的作用。)

struct foo()
{
    foo(){
        throw "bar";
    }
    ~foo(){
        /*am I called*/
    }
};

foo f;
Run Code Online (Sandbox Code Playgroud)

c++

6
推荐指数
1
解决办法
1624
查看次数

标签 统计

c++ ×2

destructor ×1

exception ×1

memory-leaks ×1