Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - static class members: destructors http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   Static Class Members: Destructors
   In  the Vector example the constructor allocated memory from the  heap
   with the new operator. This memory remains allocated until  explicitly
   freed.  C++ provides a complementary function, the  destructor,  which
   can  be used to ensure that this is carried out. A destructor has  the
   same  name  as  the  class (and the constructor)  except  that  it  is
   preceded by a tilde (~).

   If  a  destructor  is  defined  for  the  class,  the  compiler   will
   automatically  call it whenever an object belonging to the class  goes
   out  of scope (is destroyed). There is one exception to this rule.  If
   the  object  has  been  created using the  new  operator  it  must  be
   explicitly  destroyed  using  the delete operator  in  order  for  the
   destructor to be invoked.

   Destructors  allow the programmer to ensure that any tidying up,  such
   as  the  freeing  of  dynamically allocated  memory,  is  carried  out
   automatically.

   The  destructor for a class object does not destroy the object  itself
   when  called.  This  means that it is possible explicitly  to  call  a
   destructor without reclaiming the allocated memory used by the object.
   This  is quite often required when dealing with a class  object  which
   has been allocated at a specific address using operator new.


Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson