Jump to content

Talk:Destructor (computer programming)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Sebastiangarth (talk | contribs) at 06:15, 3 May 2010 (Merging virtual destructor description to Examples -> C++ section?: Yes, the example assumes a modicum of proficiency, as it should). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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)[reply]

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)[reply]

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

by Notinlist (talk) 04:14, 3 May 2010 (UTC)[reply]

For the average C++ programmer, I think the existing example is fairly straightforward. Also, the code you posted is frankly not a good example of best practices, eg: using signed integers without sanity checks, improper usage of RAII, inconsistent naming conventions, no encapsulation, superficial objectification, etc... Sebastian Garth (talk) 06:15, 3 May 2010 (UTC)[reply]