In the below code, when deleting a object of base class in destruct class, none of the destructors of any class are getting executed.What may be the reason? Which destructor is "delete obj" calling ?
#include <iostream>
using namespace std;
class base;
class destruct{
public :
destruct() {
cout<<"Destruct Constructor called"<<endl;
}
void destructObj(base* obj) {
delete obj;
}
~destruct() {
cout<<"Destruct Destructor called"<<endl;
}
};
class base {
int runs;
public:
base(){
cout<<"Constructor called"<<endl;
}
~base(){
cout<<"destructor called"<<endl;
}
};
int main() {
base *obj = new base();
destruct *desObj = new destruct();
desObj->destructObj(obj);
return 0;
}
I expected atleast one of the destructors running of any class. Please help me find out how the delete operator would be working in this case.
base.base's destructor is only called, but if you were to passdesObjtodestructObjit would be UB.deletethedesObjpointer, so that object won't be destructed.deletetheobjpointer. In another function or directly inmain, it will still be deleted just the same. Perhaps the problem you have isn't replicated by the code you show? Is that really a proper minimal reproducible example that replicates the problem you have? What is the output of the program you show? What output did you expect? And please take some time to refresh how to ask good questions, as well as this question checklist.basethere's no inheritance here.warning: deleting pointer to incomplete type 'base' may cause undefined behavior [-Wdelete-incomplete].