#include<iostream>
using namespace std;
class test
{
int a;
public:
test()
{
a=0;
}
test operator ++(int)
{ a++;
return *this;
}
void display()
{
cout<<a;
}
~test() {
cout << "DES" << endl;
}
};
int main()
{
test t,t1;
t1 = t++;
t.display();
system("pause");
}
The output that i get is :
DES
1Press any key to continue...
DES
DES
Why does the destructor work before ?
When you are using
t++a temporary object is created. This is the reason you have 3 destructors called for 2 variables. If you use++t, temporary object would not be created. Which, other than the operator precedence, is the main difference between pre- and post- incrementing.