Clarification of Destructors in C++ class's

Started by
4 comments, last by smart_idiot 18 years, 11 months ago
Is the destructor called automaticly atthe end of whatever scope it is in?
-Matt S.
Advertisement
Yes it is. You don't call it.
Thanks for the quick reply
-Matt S.
If I am not mistaken, I don't think it's called automatically.

You have to called "delete" or else you will get a dangling pointer which leads to memory leak.

"You need a long-term goal: keep trying and make a small step every day. Don't give up! Keep going, and that will be the shortcut to success." -- Nobuo Uematsu (Final Fantasy Composer)
I forgot to mention one thing.

The destructor can be called automatically when the pointer is out of scope if the pointer is an auto_ptr type.

This is the template spec for auto_ptr:

template <class T> class auto_ptr

It is declared in the header <memory>

But auto_ptr does not support reference couting, so the pointer can have only 1 owner at a time.

Hope that helps
"You need a long-term goal: keep trying and make a small step every day. Don't give up! Keep going, and that will be the shortcut to success." -- Nobuo Uematsu (Final Fantasy Composer)
He didn't say anything about pointers. Assuming

 {  MyClass bar;    // bar.~MyClass() gets invoked automatically here. }


then the answer is indeed yes.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement