By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,725 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

"delete this" not calling overridden delete?

P: 1
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"?
Oct 25 '07 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
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:
Expand|Select|Wrap|Line Numbers
  1. //Assume B derives from A
  2.  
  3. void B::Delete() {
  4. A::delete this;          //delete your base class
  5. delete this;              //delete ourselves
  6. }
  7.  
The same problem arises with operator overloading since the operators are not inherited:
Expand|Select|Wrap|Line Numbers
  1. B B::operator=(B rhs)
  2. {
  3.     A::operator=(this);     //assign our base class members
  4.     //now assign our members
  5.     //etc...
  6. }
  7.  
Oct 28 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.