dimanche 29 mars 2015

c++ destructor getting called 2 times on the same object?

I have the following code snippet:



#include<iostream>
using namespace std;

class Test {

public:

Test() { cout << "Constructor is executed\n"; }
~Test() { cout << "Destructor is executed\n";}
void show() { this->Test::~Test(); }
};

int main() {

Test t;
t.show();
return 0;
}


Here is the output:



Constructor is executed
Destructor is executed
Destructor is executed


Question: If t.show() has already called the destructor on "this" object(i.e. current object), which causes "destructor is executed" to be displayed once, then what causes it to be displayed for the second time? Which object is getting destroyed in that case ?


Aucun commentaire:

Enregistrer un commentaire