I have a question regarding exceptions in contructors.
Picture that I have the following class:
Code:
class BaseObject
{
public:
BaseObject();
virtual ~BaseObject();
};
Now I have a simple derived class as follows:
Code:
class DerivedObject : public BaseObject
{
DerivedObject();
virtual ~DerivedObject();
};
What happens if an exception is thrown within the constructor of the DerivedObject, like so:
Code:
DerivedObject::DerivedObject()
: BaseObject()
{
// Contructor - do something
try
{
...
...
// EXCEPTION THROWN HERE
...
}
catch(...)
{
// Free newly created heap objects
}
}
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?
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.