1

Below is my sample code, where I overrided the delete operator for derived class.

My aim is I want to delete the object for both base and derived class in delete operator. Basically I want to hold the address of both class till delete operator get called.

Right now I am using atomic operation to synchronize, basically my destructor didn't get called, when I am executing some functionality of class. Now, I want to get ride of that atomic operation. So, I will catch the object address and hold there in delete operator till it is safe to do. Destructor will be called by others.

So, which is the correct place for delete operator, base class or derived class? When I used delete operator in both class only derived delete operator get called.

#include <iostream>
using namespace std;

class base
{
    public:
     int l;
     base() {cout << " base const" << endl;}
     ~base() { cout << " base descr" << endl;}
};

class derived: public base
{
public:
 int k;
 derived() {cout << " derived const" << endl;}
 ~derived() { cout << " derived descr" << endl;}
 static void operator delete (void *p) {
      return delete[] static_cast<char*>(p);
  }

};

int main () {
  derived *p = new derived;
  p->k = 7;
  p->l = 8;
  cout << p <<" " << p->k << " " <<p->l << endl;
  delete p
}
eswaat
  • 633
  • 1
  • 11
  • 27

0 Answers0