"delete this" not calling overridden delete?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MorgyTheMole
    New Member
    • Oct 2007
    • 1

    "delete this" not calling overridden delete?

    I have a base class:

    class A {
    protected:
    static void* operator new (size_t size);
    void operator delete (void *p);
    public:
    void Delete();
    }

    and then a subclass that calls Delete(), here are those function definitions (in a nutshell):

    void* A::operator new (size_t size) {
    return AllocMem(size);
    }

    void A::operator delete (void *p) {
    LOG("Working!") ;
    FreeMem(p);
    }

    void A::Delete() {
    delete this;
    }

    My problem is that when I call Delete from the subclass, I can break in VC++ to the delete this function call, but don't get any breaks on the overridden operator, nor any log output. So my delete operator is never being called (my new operator IS being called, I checked)

    Am I doing something wrong? Why wouldn't my delete operator get called, does it have something to do with explicitly using "delete this"?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Since you are using a subclass, I assume that there are ot virtual functions around.

    On that assumption, you have a derived object and you are using the object directly. In this case, the derived function that overrides the base function has hidden the base function so you will need to call it directly:
    [code=cpp]
    //Assume B derives from A

    void B::Delete() {
    A::delete this; //delete your base class
    delete this; //delete ourselves
    }
    [/code]

    The same problem arises with operator overloading since the operators are not inherited:
    [code=cpp]
    B B::operator=(B rhs)
    {
    A::operator=(th is); //assign our base class members
    //now assign our members
    //etc...
    }
    [/code]

    Comment

    Working...