its an example so its ok here, but generally you don't want to explicitly call a dtor that way. scope it and let it self-destruct to test it. a simple way:
void testdtor()
{
Foo f;
}
main()
{
testdtor();
}
If you report everyone who tells you a truthful answer, you will soon not have any help... the report function is not a dislike button.
The lifetime of the object ends when control enters its (non-trivial) destructor.
Explicitly calling the destructor does not suppress the implicit call when the object goes out of scope. Consequentially, the implicit call to the destructor is made on "not-an-object" and so the behavior is undefined. https://en.cppreference.com/w/cpp/language/lifetime
There are rare cases where explicit calls to the destructor are correct and even required. A beginner shouldn't bother with them.
I know you're right, I was merely giving an example to show how to define the constructor in a class and showing that it did indeed work when you call it. My bad, should have done that instead:
The most common use-case is when we want to separate acquiring and releasing memory from creation and destruction of objects to be placed in that memory.
For instance, when we do a pop_back() on a vector, the object at the back of the sequence must be destroyed; but the reserved memory remains allocated. Every allocator aware container needs to separate these two concerns.
A vector is a class of a class? That makes sense. Thanks a lot for the example. So when you resize a vector you're really instantiating more objects inside the vector class. What about reserve? I need to look at how vectors are implemented.
If a vector is not a class of a class then the individual elements wouldn't have ctors and dtors.. only the entire vector would..
And back to what mbozzi said. He said there can be uses for explicit calls to the destructor. Are there? Or did he perhaps mean what you said - explicit calls to releasing memory.
See the discussion here for a kind of pessimized example of an explicit destructor call: http://www.cplusplus.com/forum/beginner/210637/
(Where the issue of exception safety is ignored in the interest of clarity.)