Override delete operator

前端 未结 3 495
有刺的猬
有刺的猬 2021-01-18 12:08

I want to override delete operator in my class. Here\'s what I am trying to do,but not succeeding.

class Complex{              
    void *operator new(size_t         


        
相关标签:
3条回答
  • 2021-01-18 12:25

    Deleting through delete is quite strange, but deleting a void* is UB.

    Also, size_t is not a built-in type: it is define in <cstddef>.

    This can be fine:

    #include <cstddef>
    
    class Complex
    {              
        void *operator new(size_t s);
        void operator delete(void *ptr);
    };
    
    void* Complex::operator new(size_t s)
    { return new char[s]; } //allocate a buffer of s bytes
    
    void Complex::operator delete(void *ptr)
    { delete[] static_cast<char*>(ptr); } //deallocate the buffer
    

    Practically, we allocate/deallocate a buffer of appropriate size coherently in new / delete.

    In new, we ask the system to give us the bytes we need. I used char[s] since char is the unit of memory size_t measures: sizeof(char) == 1 by definition.

    In delete we have to give back to the system the bytes identified by ptr. Since we allocated them as char[], we have to delete them as char[], hence the use of delete[] and the cast to char*.

    0 讨论(0)
  • 2021-01-18 12:28

    Your declarations are correct. The problem is in the code that implements your operator delete: it uses the keyword delete instead of calling the global operator delete. Write it like this:

    void Complex::operator delete(void *ptr) {
        ::operator delete(ptr);
    }
    

    That's assuming that your operator new used the global operator new.

    0 讨论(0)
  • 2021-01-18 12:31

    As the error message indicates, you can't delete a void*. Try this:

    // See http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=40
    
    #include <new> // for size_t
    
    class Complex
    {
    public:
     Complex() {}
     ~Complex() {}
     static void* operator new (size_t size) {
      return new char[size];
     }
     static void operator delete (void *p) {
      return delete[] static_cast<char*>(p);
     }
    };
    
    int main () {
      Complex *p = new Complex;
      delete p;
    }
    
    0 讨论(0)
提交回复
热议问题