class String { private : char * s; int size; public : String( char *); // constructor ~String(); // destructor }; String::String( char * c) { size = strlen (c); s = new char [size + 1]; strcpy (s, c); } String::~String() { delete [] s; } |
When do we have to compose a client characterized destructor?
On the off chance that we don’t compose our own destructor in class, compiler makes a default destructor for us. The default destructor works fine except if we have progressively allotted memory or pointer in class. At the point when a class contains a pointer to memory assigned in class, we ought to compose a destructor to deliver memory before the class occurrence is annihilated. This should be done to keep away from memory spill.
Could a destructor be virtual?
Indeed, truth be told, it is dependably really smart to make destructors virtual in base class when we have a virtual capacity. See virtual destructor for additional subtleties.
You might jump at the chance to take a test on destructors.
Also Read: How to Install Minecraft on Linux?