If I do
Bat::Bat() : m_member_str(new std::string("Am I freed?"))
{
throw std::runtime_error("oops");
}
Is the newly allocated std::string freed? I was thinking it might be because the destructor is not called.
I am not using a std::string, but my own class, just showing it as an easy example.
This example is the classic use case for smart pointers.
Batis not fully constructed, so the destructor won’t be called, but the destructor form_member_strand all other fully constructed members will be. If you don’t want an ugly block liketry { foo(); } catch (...) { delete m_member_str; }, you’ll have to get comfortable with RAII.std::auto_ptrorboost::scoped_ptrwill help you in C++03, or their equivalents in C++11. There is very little disadvantage in using them for owned member pointers.