473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

"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"?
Oct 25 '07 #1
1 1649
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

2
by: aiKeith | last post by:
I have a really stupid problem I'm hoping to get help with. The problem occurs if I delete rows from a dynamically built datatable. ie: CreateDataTable(); // creates the structure of the...
10
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
6
by: R.Z. | last post by:
i'm using a class from some api that is said to automatically call its destructor when its out of scope and deallocate memory. i create instances of this class using "new" operator. do i have to...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.