I'm working on RVO/Copy-Constructor/Destructor and checking code by randomly. I'm little confused here why destructor called three time..??
#include <iostream>
using namespace std;
class A{
public:
A(){
cout << "Simple Constructor" << endl;
}
A(const A& obj){
cout << "Copy Constructor " << endl;
}
A operator =(A obj){
cout << "Assignment Operator" << endl;
}
~A(){
cout << "Destructor " << endl;
}
};
A fun(A &obj){
cout << "Fun" << endl;
return obj;
}
int main(){
A obj;
obj=fun(obj);
cout << "End" << endl;
return 0;
}
Output:
Simple Constructor // ok
Fun // ok
Copy Constructor // ok for =
Assignment Operator // ok
Destructor // ok for =
Destructor // why here destructor called?
End // ok
Destructor // ok for main
I was expecting Destructor
to be called two times.
One for (=) operator's
Object.
Second one for int main()'s
object.
Why is it being called the third time? And how?