Talk:Destructor (computer programming)
Which language returns value from a dtor function? (as written in the introduction) I kow abt c++. It does not return any value from dtor fuction call.
PHP5
PHP5 introduces destructors. Probably should be covered here. I came here hoping someone would be ahead of me in sorting out: is there (as in C++) a guarantee that the destructor runs promptly when a statically declared object passes out of scope, or might it run at some later time? There are techniques in C++ programming that rely on this aspect of destructors, I'm wondering whether they will work in PHP5.- Jmabel | Talk 17:33, 21 February 2008 (UTC)
- This article would suggest that it works, starting with PHP 5.2, but I don't know how definitive it is. - Jmabel | Talk 17:40, 21 February 2008 (UTC)
Merging virtual destructor description to Examples -> C++ section?
AFAIK, only C++ really has virtual dtors, so wouldn't it make more sense to merge that with the Examples -> C++ section? Not to mention that the virtual dtor section really only references C++ anyway. Thoughts?
Sebastian Garth (talk) 17:46, 19 September 2009 (UTC)
I think that the C++ example is too difficult for explaining the concept of destructor. I seriously doubt the explanatory value of it. I recommend a much simpler example like this:
class Board {
public:
Board(int w_, int h_) // Constructor of Board
: w(w_) // Save the w_ parameter to the w member variable
, h(h_) // Like the above
, cells(new Cell[w*h]) // Allocate memory for board cells, w*h pieces.
{
// ... some program
}
// ... Some methods
~Board() // Destructor of Board
{
delete[] cells; // Free the memory
}
private:
int w,h; // width and height
Cell *cells; // cells. Cell is a type which is not explained in this example
};
// ...
int main()
{
Board b(8,8); // <--- Constructor for b is called (Board::Board()), created as automatic variable
Board *x = new Board(8,8); // <--- Constructor for x is called (Board::Board()), allocated manually
// ...
delete x; // <--- Destructor for x is called (Borad::~Board()), destroyed manually
// ...
return 0;
} // <--- Destructor for b is called (Borad::~Board()), destroyed automatically when goes out of scope