Delete operator overloading with multiple arguments.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kapilkumawat
    New Member
    • Jan 2010
    • 1

    Delete operator overloading with multiple arguments.

    I have an requirement to overload the delete operator in C++, but it should also accept the sizeof() the object that is to be deleted. Actually I am trying to built a custom memory allocator and deallocator like a pool, which makes me to overload the delete operator.
    Small example of the problem is

    Code:
    #include <new>
    #include <iostream>
    using namespace std;
    void operator delete(void* p)
    {
    cout << "Hello" << endl;
    free(p);
    } // (1)
    void operator delete(void* p, size_t s)
    {
    cout << "World" << endl;
    free(p);
    } // (2)
    int main()
    {
    int* p = new int;
    delete p; // This cannot render to show 'Hello' or 'World'
    }
    Output
    Hello
    I want to call the delete with size operator. So please help me how can this program will give the output as World.
  • Airslash
    New Member
    • Nov 2007
    • 221

    #2
    arent they supposed to be void* for new and delete?

    also you'll need to implenet the new[] and delete[] and the new(size_t) and delete(size_t) operators as well.

    and I believe they need to be static as well.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Nothing needs to be static.

      However, an operator overload requires one of the arguments to be a user-defined type. At shown, these delete overloads will not compile.

      Comment

      Working...