The World of Mayukh Bose

<< Back to Main Page Mayukh's world: e-mail me | about
Mayukh's World: Operator Overloading With C++ Thursday, November 23, 2017
Index
  • Introduction
  • What Operators?
  • Rules
  • Assignment Operator
  • More on Assignment
  • Arithmetic Operators
  • Arithmetic with Globals
  • Increment/Decrement
  • Operator-Assignment
  • Unary Operators
  • Relational Operators
  • Bitshift/Extraction
  • Subscript Operator
  • Function Call Operator
  • Bit and Logical Ops
  • Comma Operator
  • Pointer to Member
  • new and delete Ops
  • Credits and Thanks
  • My Free Software
  • Delphi/C++ Builder
  • Pocket PC
  • FreeSpeech Chat
  • C/C++ Freebies
  • Perl
  • Python
  • Ruby
  • C++ Operator Overloading Tutorial e-mail me

    The new and delete Operators

    The new and delete keywords are actually treated by C++ as operators and hence, they can be overloaded as well. The overloaded new operator is called before the object's constructor and the overloaded delete operator is called after the object's destructor. This can be used in some real interesting ways. For instance, if a class needs to perform some special initializations, an overloaded new can help to do that. Memory checkers could use this to keep track of objects being created and deleted.
           inline void *operator new(size_t size) {
             void *ptr = malloc(size);
    	 AddTrackingInfo(ptr, size, __FILE__, __LINE__);
    	 return *ptr;
           }
    
           inline void operator delete(void *ptr) {
             RemoveTrackingInfo(ptr, __FILE__, __LINE__);
    	 free(ptr);
           }
          
    In the above code, __FILE__ and __LINE__ are macros defined by the C++ standard, that are replaced by the compiler with the appropriate source file name and line number respectively. The overloaded functions are declared as inline to give a hint to the compiler to put the function bodies in the code directly, instead of compiling them as functions. If the compiler complies with the inline directive, then AddTrackingInfo() and RemoveTrackingInfo() are called with the exact file names and line numbers of pointers being allocated or deallocated. Thus, this could help track down memory leaks.

    Another fun thing to do is to use new to tell an object if it is created dynamically or not (code courtesy of Grumpy):
            class X {
            private: 
              bool dynamic; 
              static bool is_dynamic; 
            public: 
              X() { dynamic = is_dynamic; is_dynamic = false; }
              X(const X &); 
              ~X(); 
              X &operator=(const X &); 
              bool CreatedDynamically() const {return dynamic;} 
              void *operator new(size_t size); 
            }; 
    
            bool X::is_dynamic = false; 
    
            X &X::operator=(const X &x) {
              // do NOT adjust values of dynamic and is_dynamic 
              // do NOT  assign dynamic = x.dynamic; 
              // but assign other members as required. 
              return *this; 
            } 
    
            void *X::operator new(size_t size) { 
              is_dynamic = true; 
              return ::operator new(size); 
            } 
          
    We have now come to the end of this tutorial. Hope you enjoyed it. The only thing left to do is to give due credit to folks who helped in the preparation of this tutorial.


    <<Previous: Pointer to Member Operator ^Up to Mayukh's World^ Next: Credits and Thanks >>

    Copyright © 2004 Mayukh Bose. All rights reserved.
    This work may be freely reproduced provided this copyright notice is preserved.
    OPTIONAL: Please consider linking to this website (http://www.mayukhbose.com/) as well.