Calling a member function from an overloaded delete operator?

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).

So the design is wrong, right? (If it is right, why is it right?)

+5
source share
3 answers

( Window::destroy -):

3.8p1: T , : if T - , [...]

3.8p5: [...] , , , , , , [...] , .... , POD, undefined , : - - [...]

( )

+6

, ( ++) .

new delete, , , .

, , ... , MS , ++ (au contraire, ) , ( 2002 ) , .

0

, .

, COM- delete , obj->destroy(). , "", 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.

0
source

All Articles