In the following program, how is p
getting deleting twice, as it is pointing to same memory location?
#include <iostream>
using namespace std;
class Test {
public:
int *p;
Test() {
cout << "Constructor is executed\n";
}
~Test() {
cout << "Destructor is executed\n";
delete[] p;
cout << "p deleted\n";
}
void make() {
p = new int[5];
}
//here destructor is called expllicitly
void show() {
this->Test::~Test();
}
};
int main() {
Test t;
t.make();
t.show();
return 0;
}
Output:
Constructor is executed
Destructor is executed
p deleted
Destructor is executed
p deleted