
Thank you. I'm mistaken. On Wed, May 7, 2008 at 11:10 AM, Steven Watanabe <watanabesj@gmail.com> wrote:
AMDG
shiwei xu wrote:
Your question is very interesting. I thought it before. In fact I don't know a perfect solution. If the constructor of an object throws, should the destructor be called?
class Foo { private: A m_a; B m_b; C m_c;
public: Foo() { m_a.init(); m_b.init(); thow std::exception("error"); m_c.init(); } ~Foo() { ... } };
Suppose we initialized m_a and m_b. m_c was uninitialized when the exception throws. If the destructor is called, it may cause a crash. If the destructor isn't called, the allocated memory of m_a and m_b will be leaked.
I choose to call the destructor because I think that a crash is easy to be detected and be solved.
This is not in accord with normal C++ semantics. It is the responsibility of the constructor to make sure that if it fails, it cleans up any resources that it allocated.
In Christ, Steven Watanabe