我有一些问题来处理派生类中的构造函数异常.当派生类构造函数抛出错误时,父类已经分配了一些对象.是否会调用父类析构函数?
例:
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)