I saw a game developer overload the new and delete operator on YouTube. His name is Cherno.
void* operator new(size_t size){
return malloc(size);
}
void operator delete(void* pointer, size_t size){
free(pointer);
}
Well, now what I did is I created a class:
struct MyClass{
int my_int;
MyClass(int a): my_int{a}{}
~MyClass(){cout << "MyClass is destructed" << endl;}
};
And I created a heap allocated object of that class in the main() function
int main(){
MyClass* cls = new MyClass{10};
// free(cls); //Doesn't call the destructor
delete cls; //Does call the destructor
return 0;
}
Output:
MyClass is destructed
So, here is my question. Why does the delete call the destructor and the free() doesn't, even if all delete does is that it uses the free() internally (I assume).
And I know that we shouldn't use free() on objects that are allocated using new operator. But even in this case the new operator uses malloc() internally