When I run the following code in C++:
#include <iostream>
using namespace std;
class Test
{
public:
string name;
Test();
Test(string pname);
~Test();
Test operator+(Test right);
};
Test Test::operator+(Test right)
{
Test neo_name;
neo_name.name = name + "-" + right.name;
return neo_name;
}
Test::Test()
{
}
Test::Test(string pname)
{
name = pname;
}
Test::~Test()
{
cout << "~ " << name << endl;
}
int main()
{
Test D1("a");
Test D2("b");
Test D3;
D3 = D1 + D2;
return 0;
}
I notice that the + operator overload method destroys the "a-b" (neo_name) and "b" (right) objects.
(1) ~ a-b
(2) ~ b
(3) ~ a-b
(4) ~ b
(5) ~ a
Does anyone know how to destroy each object once?
At the end, I expect an output like:
(3) ~ a-b
(4) ~ b
(5) ~ a
Thanks!