$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-05-06 23:10:07
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