So i'm trying to create a base class that has a default deallocation function for that type of base classes. Depending on how I delete a derived object I'm seeing different behaviour, maybe anyone can shed some light on why I'm not seeing my override working in the below commented cases where the custom deallocation is not invoked:
#include <iostream>
struct B {
void operator delete(void* ptr) {
std::cout << "B's operator delete" << std::endl;
::operator delete(ptr);
}
};
struct D : B {
};
template<typename T>
class E {
public:
E(T* inst) {
//delete inst; // invokes the operator delete override
T::operator delete(inst); // invokes the operator delete override
//operator delete(inst); // does not invoke the operator delete override
}
};
int main() {
D* dp = new D();
E<D>* ep = new E<D>(dp);
delete ep;
}
I'm guessing that the last attempt (invoking operator delete) without T:: uses the global deallocation function instead of my override, but why is that when delete inst
is working just fine without having to specify T::?
I was expecting all three statements to actually invoke the delete operator for the object if it has been overriden. Can this be controlled through anything or is this correctly following the C++ ABI?