Don't understand the
(a, b is the object of the class a) cout
But cout<(a + b); Just an error
I overloaded + the return value is an object, how an error says no matching with the operands of
The code shown in figure
If the overloaded
Why to solve, thank you!
CodePudding user response:
Const A& a
CodePudding user response:
reference 1st floor Italink response: const A& A Thank you bosses In a word, strong! But why not add const deny? CodePudding user response:
People think because no copy function, if there is no custom copy function compiler also provided no complains, because no & amp; , add a & amp; Is the meaning of the same name will not copy constructor, the call so not in error CodePudding user response:
CodePudding user response:
But now it is to write A or A const A& A const or aa is right Writing A& A, an error And in writing A& When a If don't write an error when the call A, c=A + b. Cout But to write Cout <(a + b) Just an error Strange Delete the situation after the constructor or unchanged I wrote It's too hard to CodePudding user response:
reference DXGZG reply: 3/f People think because no copy function, if there is no custom copy function compiler also provided no complains, because no & amp; , add a & amp; Is the meaning of the same name will not copy constructor, the call in the error for not Thank you, but an error that is Not matching with the operands of The operand types for STD: : ostream Upstairs, I just tried the results Oh wow, difficult CodePudding user response:
refer to 6th floor m0_44977681 response: Quote: refer to the third floor DXGZG response: People think because no copy function, if there is no custom copy function compiler also provided no complains, because no & amp; , add a & amp; Is the meaning of the same name will not copy constructor, the call in the error for not Thank you, but an error that is Not matching with the operands of The operand types for STD: : ostream Upstairs, I just tried the results Wow, difficult oh copy constructor is A (const & amp; A) CodePudding user response:
A, b; A& C=a + b; C.a.=1; This sentence to modify the c, c is a reference to a variable, reference is a + b back to a temporary variable, the return value will be at the end of the expression to calculate destructor, so this line of code is wrong CodePudding user response:
refer to the second floor m0_44977681 response: Quote: refer to 1st floor Italink response: const A& A Thank you bosses In a word, strong! But why not add const deny? Because of (a + b) returns a temporary variable, the temporary variables take less than you, and the compiler default temporary variables is const, so you have to put & lt; CodePudding user response: Because the const T& Is called the universal reference, not only can bind an lvalue, can also be bound right value, a temporary variable without a const reference binding, his lifetime will be over, and will extend its lifetime, CodePudding user response:
reference dirty, 9/f, don't reply: Quote: refer to the second floor m0_44977681 response: Quote: refer to 1st floor Italink response: const A& A Thank you bosses In a word, strong! But why not add const deny? Because of (a + b) returns a temporary variable, the temporary variables take less than you, and the compiler default temporary variables is const, so you have to put & lt; Thank you for your answer! Basic understand! CodePudding user response:
references to the tenth floor truth is right or wrong response: because const T& Is called the universal reference, not only can bind an lvalue, can also be bound right value, a temporary variable without a const reference binding, his lifetime will be over, and will extend its lifetime, Yes, yes, thank you for your answer, very clear! CodePudding user response:
Many say Although the const T& Can be bound to the right value, but he is read-only, there is a limit to use, so the c + + 11 introduced rvalue references, not only can prolong the lifetime of the temporary variables, and can read and write The code below Int test () { Return 10; } Int main () { Int& & B=test (); B=11; } CodePudding user response:
refer to the 13th floor truth is right or wrong response: , more sayingAlthough the const T& Can be bound to the right value, but he is read-only, there is a limit to use, so the c + + 11 introduced rvalue references, not only can prolong the lifetime of the temporary variables, and can read and write The code below Int test () { Return 10; } Int main () { Int& & B=test (); B=11; nullnullnullnullnullnullnullnullnullnullnullnullnullnull