Does dynamic_cast work inside overloaded operator delete ?

Posted by iammilind on Stack Overflow See other posts from Stack Overflow or by iammilind
Published on 2011-06-22T05:55:36Z Indexed on 2011/06/22 8:22 UTC
Read the original article Hit count: 201

Filed under:
|
|

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* ?

© Stack Overflow or respective owner

Related posts about c++

Related posts about dynamic-cast