Does dynamic_cast work inside overloaded operator delete ?
- by iammilind
I came across this:
struct Base {
void* operator new (size_t);
void operator delete (void*);
virtual ~Base () {} // <--- polymorphic
};
struct Derived : Base {};
void Base::operator delete (void *p)
{
Base *pB = static_cast<Base*>(p);
if(dynamic_cast<Derived*>(pB) != 0)
{ /* ... NOT reaching here ? ... */ }
free(p);
}
Now if we do,
Base *p = new Derived;
delete p;
Surprisingly, the condition inside the Base::delete is not satisfied
Am I doing anything wrong ? Or casting from void* looses the information of Derived* ?