I have the following code:
#include <iostream>
using namespace std;
class Foo {
int data;
public:
Foo(int d = 0) {
data = d;
}
~Foo() {
cout << data;
}
};
int main() {
Foo a;
a = 20;
return 0;
}
The output of this code is 2020. I think what happens is, a temporary object a is created. Once the assignment operator is used to assign a the value of 20, the destructor is called and a 20 is printed. Then the main function reaches the return and the destructor is called a second time, printing 20 again.
Am I right?