I have a question regarding exceptions in contructors.
Picture that I have the following class:
Now I have a simple derived class as follows:Code:class BaseObject { public: BaseObject(); virtual ~BaseObject(); };
What happens if an exception is thrown within the constructor of the DerivedObject, like so:Code:class DerivedObject : public BaseObject { DerivedObject(); virtual ~DerivedObject(); };
I am familiar with catching exceptions within constructors to prevent memory loss. However, in this specific case, will the destructor of the BaseObject be called automatically after the exception is thrown?Code:DerivedObject::DerivedObject() : BaseObject() { // Contructor - do something try { ... ... // EXCEPTION THROWN HERE ... } catch(...) { // Free newly created heap objects } }
My guess is that it won't, but I'm not sure. Should I ensure that I free any memory held by BaseObject in the catch block of DerivedObject constructor?
Edit : Oops there was an exception in the title of this thread.



LinkBack URL
About LinkBacks


