This article on Binary-compatible C ++ Interfaces contains code:
class Window { public: // ... virtual void destroy() = 0; void operator delete(void* p) { if (p) { Window* w = static_cast<Window*>(p); w->destroy(); // VERY BAD IDEA } } };
For me, this seems wrong: it operator delete()works with raw memory to free it. The object's destructor has already been called, so the call destroy()works with the phantom object (if it works at all). Indeed: why operator delete()accept void*, not a Window*(in this case).
operator delete()
destroy()
void*
Window*
So the design is wrong, right? (If it is right, why is it right?)
( Window::destroy -):
Window::destroy
3.8p1: T , : if T - , [...]3.8p5: [...] , , , , , , [...] , .... , POD, undefined , : - - [...]
3.8p1: T , : if T - , [...]
T
3.8p5: [...] , , , , , , [...] , .... , POD, undefined , : - - [...]
( )
, ( ++) .
new delete, , , .
, , ... , MS , ++ (au contraire, ) , ( 2002 ) , .
, .
, COM- delete , obj->destroy(). , "", destroy. !
delete
obj->destroy()
It would be much better to simply declare the removal of a private operator, so the user cannot delete objects and must correctly determine the correct path.