As you know, if an exception is thrown in the class constructor, the object is not considered created and its destructor is not called.
Consider this class:
class A { class B { public: void operator =(char* v) { _v = v; } ~B() { delete[] _v; } private: char* _v; }; B _b; public: A() { _b = new char[10]; throw 1; } ~A() { std::cout << "~A()" << std::endl; } }; int main() { try { A a; } catch(...) { } } In this case, there will be no memory leak. But why will a class B destructor be called? Where do we get to it? After all, the object containing it will not be destroyed.
Awill be considered not created, and an object of classB, which is a field of the created object of classA, will be created. Accordingly, when throwing an exception from the classAconstructor, the code generated by the compiler will be invoked to destroy the already created classAfields. - VTT