小编ren*_*nan的帖子

派生类构造函数中的异常

我有一些问题来处理派生类中的构造函数异常.当派生类构造函数抛出错误时,父类已经分配了一些对象.是否会调用父类析构函数?

例:

class A
{
  A() { /* Allocate some stuff */ };

  virtual ~A() { /* Deallocate stuff */ };
};

class B : public A
{
  B() 
  { 
    /* Do some allocations */
    ...

    /* Something bad happened here */
    if (somethingBadHappened) 
    {
      /* Deallocate B Stuff */
      ...

      /* Throws the error */
      throw Error; /* Will A destructor be called? I know B destructor won't */
    };

  };

  ~B() { /* Deallocate B Stuff */ }; …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance constructor destructor exception

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

标签 统计

c++ ×1

constructor ×1

destructor ×1

exception ×1

inheritance ×1