When is a good time to explicitly call a destructor ?

T

Timothy Madden

I know I can call destructors if I want (probably with their fully qualified
name).

However I don't see the point here. When and where would I want to call
destructors ?
Destructors are not designed to destruct one object twice and if I call the
destructor myself then it will get called a second time when the object goes
out of scope (or out of its lifetime)

Thank you
"Timothy Madden"
Romania
 
I

Ivan Vecerina

Timothy Madden said:
I know I can call destructors if I want (probably with their fully
qualified
name).

However I don't see the point here. When and where would I want to call
destructors ?
Destructors are not designed to destruct one object twice and if I call
the
destructor myself then it will get called a second time when the object
goes
out of scope (or out of its lifetime)
When you want to dissociate the allocation/release of memory from the
construction/destruction of stored object(s).

Consider the implementation of std::vector. When member function pop_back()
is called, the last object stored in the vector must be destroyed (it is
a requirement of the standard). However, the memory allocated for the
vector will not (and shall not) be released or resized.
pop_back() will therefore use an explicit destructor call.


Note that the opposite operation (explicit call of a constructor)
is achieved by using the placement-new syntax:
new (objAddress) MyClass(...optional constructor params...);
Such an explicit construction will be used by push_back() and
other methods of std::vector that add new elements.


Cheers,
Ivan
 
D

David Harmon

On Thu, 18 Nov 2004 15:59:42 +0200 in comp.lang.c++, "Timothy
Madden said:
I know I can call destructors if I want (probably with their fully qualified
name).

However I don't see the point here. When and where would I want to call
destructors ?

You don't. It is _very_ unusual for that to be the right thing.

This issue is covered in Marshall Cline's C++ FAQ. See the topic:
"[11.5] Should I explicitly call a destructor on a local variable?"
It is always good to check the FAQ before posting. You can get the
FAQ at:
http://www.parashift.com/c++-faq-lite/
 
T

Timothy Madden

Ivan Vecerina said:
When you want to dissociate the allocation/release of memory from the
construction/destruction of stored object(s).

Consider the implementation of std::vector. When member function pop_back()
is called, the last object stored in the vector must be destroyed (it is
a requirement of the standard). However, the memory allocated for the
vector will not (and shall not) be released or resized.
pop_back() will therefore use an explicit destructor call.


Note that the opposite operation (explicit call of a constructor)
is achieved by using the placement-new syntax:
new (objAddress) MyClass(...optional constructor params...);
Such an explicit construction will be used by push_back() and
other methods of std::vector that add new elements.
You are, indeed, correct.
Thank you

"Timothy Madden"
Romania
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top