ctor throws an exception case

P

puzzlecracker

When ctor throws an exception, dtor is not called for that object
(correct me if I am wrong) - then how would already allocated memebers
de-allocated?

Thanks.
 
A

Andre Kostur

When ctor throws an exception, dtor is not called for that object
(correct me if I am wrong) - then how would already allocated memebers
de-allocated?

Correct. If an exception propogates out of a ctor, the dtor for that
object will not be invoked as that object had not completed construction.
Any member variables of that object that had ben completely constructed
will be destroyed in the reverse order that they were constructed in (as
normal).

Note that the destructor of a pointer-to-object doesn't do anything...
that's what std::auto_ptr (and other relatives of it) are for.
 
P

peter koch

puzzlecracker skrev:
When ctor throws an exception, dtor is not called for that object
(correct me if I am wrong) - then how would already allocated memebers
de-allocated?

Thanks.

Is that not in the faq? Because every object constructed will be
destructed when unwinding:

class A
{
....
}
class B
{
....
}
class D: B
{
A a;
D() {throw;}
}

Above B::~B and A::~A will both be called (assuming A and B's
constructor does not throw).

/Peter
 
A

Alf P. Steinbach

* puzzlecracker:
When ctor throws an exception, dtor is not called for that object
(correct me if I am wrong) - then how would already allocated memebers
de-allocated?

Please ask in [comp.lang.c++.moderated] right away, if you haven't already.
 
P

peter koch

Alf P. Steinbach skrev:
* puzzlecracker:
When ctor throws an exception, dtor is not called for that object
(correct me if I am wrong) - then how would already allocated memebers
de-allocated?

Please ask in [comp.lang.c++.moderated] right away, if you haven't already.
Ouch! So he already asked there? I had some kind of a dejá vù
experience, but a quick check did not show anything.

/Peter
 
D

Diego Martins

puzzlecracker said:
Please ask in [comp.lang.c++.moderated] right away, if you haven't already.

Why? are you not qualified to answer it?

he clearly called you a cross-poster. I am sure you got that!

dissimulation is futile :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top