Delete Operator overloading with multiple arguments.
Hi,
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
#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.
Thanks in advance for my help.
Re: Delete Operator overloading with multiple arguments.
you have no "direct" control on the way the delete operator is invoked; thus, even if you can define an operator delete with as many arguments as you like (maybe the void* parameter is required anyway...) you will not be able to use it as you probably would like...
by the way, in your case passing the size of the object to delete seems also semantically wrong being the size of the object something dependent on the allocation procedure.
Therefore, your new operator should allocate both object memory and all the data your allocator needs to handle object memory lifetime.