using namespace std;
The class Test
{
Public:
The Test (int=0);
//copy constructor
The Test (const Test&);
Const Test& The operator + (const Test&);
Test& Operator=(const Test&);
Int operator () ();
Private:
Int Number;
};
//the constructor
The Test: Test (int Number_Parameter) : Number (Number_Parameter) {}
//copy constructor
The Test: Test (const Test& Test)
{
Number=test. The Number;
}
/* arithmetic function of a class */
Const Test& The Test: : operator + (const Test& Test)
{
Int Number_Parameter=0;
Number_Parameter=Number + test. The Number;
Return the Test (Number_Parameter);
}
/* assignment function of a class */
Test& The Test: : operator=(const Test& Test)
{
Number=test. The Number;
return *this;
}
/* function */class brackets
Int Test: : operator () () {return Number; }
Int main ()
{
Test_2 Test test_1 (1), (2), the Test (0);
Test=test_1 + test_2;
Cout & lt;
In the override "+" operator function, I use the "const Test&" As a return type (first encountered this as the return type, before this is not met),
But do not tally with the expected output of results, but if I use the normal "Test" as the return type, you can output the expected results "3",
The following is the result of the test, each time output is a strange number, and expectations are "3", is this why?



I by setting a breakpoint, now ruled out + operator overloading function error, then the problems in the overloaded function "=" operations, run time, among which stored Numbers "jump" :



I wondering whether overloading "+" to return to the temporary object didn't lead to mistakes,
If I have where there is no clear, please in the comments section, points out that hope bosses can give directions, thank you very much!!!!!!
CodePudding user response:
I think you have to answer your question: overloading "+" to return to the temporary object didn't lead to mistakes