相关疑难解决方法(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
查看次数

在C++中调用析构函数内的函数是一个好习惯

在析构函数中调用函数是一种好的做法,它在内部执行一些内存分配.因为这给了我访问违规和其他问题,假设

~Example(){
    Stop();
}
Run Code Online (Sandbox Code Playgroud)

在这个函数中,Stop()执行各种操作并调用其他各种函数?这是一个好习惯.有人能帮忙吗?

c++

4
推荐指数
1
解决办法
679
查看次数

标签 统计

c++ ×2

destructor ×1

exception ×1

memory-leaks ×1