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 - overloading new and delete _ http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
                       Overloading new and delete                   _
   A class may organize its own memory management by overloading the  new
   and delete operators. There are a number of special requirements  that
   must be satisfied by operator functions defined for new and delete.

   Operator new
   A  class member operator function for operator new must have a  return
   type of void* and take a first argument of type size_t. This  argument
   is AUTOMATICALLY set by the compiler with the class size in bytes.

   Operator delete
   A class member operator function for operator delete must have a first
   argument  of type void*. A second optional argument of size_t  may  be
   specified. If present, it is initialized by the compiler with the size
   in bytes of the object which is addressed by the first argument.

   Note that the default instance of operator new is always used for  the
   allocation of memory for arrays of class objects.

   The scope resolution operator may be used to force the default  global
   instance  of  operators new or delete to be used, even  when  a  class
   instance is defined, as in:

   ::delete p;

   Operators new and delete are automatically assumed by the compiler  to
   be static members of their class. This means that the this pointer  is
   undefined  within their scope and thus they can directly  access  only
   static data members of the class. This is necessary since they may  be
   invoked when no objects of the class exist.

   User Defined Type Conversions
   C++  provides a mechanism whereby the programmer may provide a set  of
   defined  type conversions for a class which can be applied to  objects
   of that class.

   The syntax for declaring a user defined conversion function is:

   operator type();

   where the return type is implied to be type, and type is itself either
   a  base  type itself, is derived from a base type or is a  class  data
   type. Conversion operators for array types are not permitted.

   A  type  conversion operator can only be specified as a  class  member
   function and cannot have a return type or argument list.

   For a conversion to integer the declaration would be:
   operator int();

   The  definition  is  then identical in format to  the  other  operator
   functions discussed earlier.

   Myclass::operator int()
   {
   }
   It  is  not normally necessary to provide a type  conversion  operator
   function  for  every  data type since  standard  conversions  will  be
   applied to the converted type automatically by the compiler.

   A  constructor which takes a single argument is a conversion  function
   from the argument type to the class object.

   The C++ language does not allow for a conversion to be reached through
   more  than one user defined conversion; however, a conversion  can  be
   applied through a combination of a single user defined conversion  and
   standard conversions.

   The  compiler  will  always  choose the  conversion  that  offers  the
   simplest  match,  with  preference being given  to  any  user  defined
   conversions.

   The compiler will generate an error if there are equally possible  but
   different conversions that can be applied to an object. The programmer
   can  resolve  such ambiguities by specifying the conversion  to  apply
   explicitly.  For instance, to force a conversion to integer  (assuming
   it is possible) use

   (int) object;
   This method is generally applicable in generating a cast of an  object
   of one type to another type.


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