In the output new called
is printed when the statement Test *m = new Test();
is executed. But we are not passing any argument to the user-defined new
function.
Can someone explain what's going on here?
#include<stdlib.h>
#include<iostream>
using namespace std;
class Test {
public:
void* operator new(size_t size);
void operator delete(void*);
Test() { cout<<"\n Constructor called"; }
~Test() { cout<<"\n Destructor called"; }
};
void* Test::operator new(size_t size)
{
cout<<"\n new called";
void *storage = malloc(size);
return storage;
}
void Test::operator delete(void *p )
{
cout<<"\n delete called";
free(p);
}
int main()
{
Test *m = new Test();
delete m;
return 0;
}