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
by Notinlist (talk) 04:14, 3 May 2010 (UTC)
- 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)
- I told that a "I recommend a much simpler example like this:", I haven't told that it is final and bulletproof solution. Yes, more checks needed. The RAII usage is not improper. The naming convention is not inconsistent. Maybe it is not your style but it does not have internal inconsistency. What do you mean by no encapsulation? Where do you see the superficial objectification? (Which may be there but you missed to give details.) And I don't like when people are talking to me "frankly". You may be right on many points, please tell them not "frankly"! Notinlist (talk) 13:20, 4 May 2010 (UTC)
- Back to the topic... My point was that the example could be about something that is not for the technology itself. Examples have better explanatory values where they are somehow connected to the real world (if it is possible of course). We are explaining the "destructor" here. The explanation should be based on simpler things, not more difficult things (like the concept of dynamic pointer, operator overloading, copy constructor, disabling copy semantics, etc ...). There is no audience for the current example. If you don't know what a destructor is you will surely not learn it from that code snippet. Notinlist (talk) 13:20, 4 May 2010 (UTC)
- My previous example was too artificial as Sebastian pointed out, but I still believe in simplicity, so I recommend the following example code for the article. I think it is better because it uses as few other features as possible, so it helps keeping the focus on the destructor.
#include <cstring>
class MyString
{
public:
MyString(const char *charArray) // Constructor
{
len = strlen(charArray); // Store length
pData = new char[len+1]; // Allocate memory
strcpy(pData,charArray); // Copy data from the parameter
}
// Place useful methods here...
~MyString() // Destructor
{
delete[] pData; // deallocate memory when destoyed
}
private:
size_t len; // data member for length
char *pData; // data member for characters
};
int main()
{
MyString s("This is a L'art pour L'art string."); // 's' is created here
// Place useful program here...
return 0;
} // 's' is destroyed automatically here, the destructor will be executed
- Note: This is not a production code, but a teaching example. Notinlist (talk) 15:38, 4 May 2010 (UTC)