Override delete operator

2020-04-05 08:18发布

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 s);
    void operator delete(void *ptr);
};

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

I get the error:

deleting void* is undefined

3条回答
Viruses.
2楼-- · 2020-04-05 08:40

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.

查看更多
三岁会撩人
3楼-- · 2020-04-05 08:57

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*.

查看更多
神经病院院长
4楼-- · 2020-04-05 09:01

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;
}
查看更多
登录 后发表回答