In C ++, the default Return Value Type of the value assignment operator is typename. For example:
Int I;
I = 3;
In fact, I = 3 returns a reference to I, so that I = 3 can be used for continuous operations, such
If (I = strlen (STR )){
... I...
}
The IF Condition Statement in the above Code not only makes a judgment but also assigns a value to I. It can also be used for Tandem assignment.
X = y = z = 3;
In fact, this operation does not require "=" to return const typename &, as long as the int value is returned. Why is it necessary to return a reference? Consider a class object with a complex data structure. If you perform this operation:
Object A, B;
Object C = (B = );
If = is not a reference type, a temporary object is generated. See the following code:
Class objecta {PRIVATE: STD: String _ name; public: objecta (const objecta & A): _ name (. _ name) {cout <_ name. c_str () <": objecta (objecta &)" <Endl; Pre };<} B = "A;" (? A ?), B (? B ?); Objecta {test (void) void static objecta ::~ Objecta ()? <Endl;: cout <_ name. c_str () <? ~ Objecta () * This; return _ name = ". _ name; "objecta: Operator = '()" <Endl; 'a) objecta & operator = "(const" objecta: objecta (string )? <The test output is as follows:
a : ObjectA::ObjectA(string)b : ObjectA::ObjectA(string)b : ObjectA::operator=()a : ObjectA::ObjectA(ObjectA&)a : ObjectA::~ObjectA()a : ObjectA::~ObjectA()a : ObjectA::~ObjectA()
The output result shows that a temporary object is generated by copying the constructor when operator = is used and then the return value is assigned to B. Therefore, it is necessary to set the return value of operator = to the reference type.
However, setting a reference type causes the following problems. The following statement is valid:
Object A, B, C, D;
(A = B) = C) = D;
What is the execution result of the program? That is, it only affects a, but actually copies D to A, B, and C without any changes. Can we stop such behavior? Yes, you only need to set the return value of operator = to const object. In this way, the compiler will give an error message when similar statements are used:
Error 2 error c2678: Binary '=': No operator found which takes a left-hand operand of Type 'const object' (or there is no acceptable conversion)
The program code is as follows:
class Object{private:int value;public:Object(int i):value(i){}const Object& operator=(const Object& obj){value = obj.value;return *this;}const Object& operator=(const int i){value = i;return *this;}int getValue(void) const{return value;}static void Test(void){Object a(1),b(2),c(3);Widget x(1), y(2), z(3);a = b = c = 4;//((a=b)=c)=4;// Error2error C2678: binary '=' :/no operator found which takes a left-hand operand of type 'const Object' /(or there is no acceptable conversion)}
However, this only imposes some restrictions on the customer's code, but is not absolutely secure? No, the customer code can still use the following statement (a = B) = C) = D; this operation:
Const_cast (A = B) = C) = 4;
However, it is more secure than using only non-const references.
To sum up, I think that in Scott Meyers's article 10th "have assignment operators return a reference to * This ", it is best to change it to "have assignment operators return a const reference to * this ". I don't know if other factors are ignored. Let's check it out. Scott Meyers wants to make the custom data type (class) conform to the default behavior of C ++, but the default behavior of C ++ is not necessarily the best. Because the statement (a = B) = C) = 4; is legal, it seems that there is no meaning, and there is no benefit, why not limit it?
In C ++, the default Return Value Type of the value assignment operator is typename. For example:
Int I;
I = 3;
In fact, I = 3 returns a reference to I, so that I = 3 can be used for continuous operations, such
If (I = strlen (STR )){
... I...
}
The IF Condition Statement in the above Code not only makes a judgment but also assigns a value to I. It can also be used for Tandem assignment.
X = y = z = 3;
In fact, this operation does not require "=" to return const typename &, as long as the int value is returned. Why is it necessary to return a reference? Consider a class object with a complex data structure. If you perform this operation:
Object A, B;
Object C = (B = );
If = is not a reference type, a temporary object is generated. See the following code:
Class objecta {PRIVATE: STD: String _ name; public: objecta (const objecta & A): _ name (. _ name) {cout <_ name. c_str () <": objecta (objecta &)" <Endl; Pre };<} B = "A;" (? A ?), B (? B ?); Objecta {test (void) void static objecta ::~ Objecta ()? <Endl;: cout <_ name. c_str () <? ~ Objecta () * This; return _ name = ". _ name; "objecta: Operator = '()" <Endl; 'a) objecta & operator = "(const" objecta: objecta (string )? <The test output is as follows:
a : ObjectA::ObjectA(string)b : ObjectA::ObjectA(string)b : ObjectA::operator=()a : ObjectA::ObjectA(ObjectA&)a : ObjectA::~ObjectA()a : ObjectA::~ObjectA()a : ObjectA::~ObjectA()
The output result shows that a temporary object is generated by copying the constructor when operator = is used and then the return value is assigned to B. Therefore, it is necessary to set the return value of operator = to the reference type.
However, setting a reference type causes the following problems. The following statement is valid:
Object A, B, C, D;
(A = B) = C) = D;
What is the execution result of the program? That is, it only affects a, but actually copies D to A, B, and C without any changes. Can we stop such behavior? Yes, you only need to set the return value of operator = to const object. In this way, the compiler will give an error message when similar statements are used:
Error 2 error c2678: Binary '=': No operator found which takes a left-hand operand of Type 'const object' (or there is no acceptable conversion)
The program code is as follows:
class Object{private:int value;public:Object(int i):value(i){}const Object& operator=(const Object& obj){value = obj.value;return *this;}const Object& operator=(const int i){value = i;return *this;}int getValue(void) const{return value;}static void Test(void){Object a(1),b(2),c(3);Widget x(1), y(2), z(3);a = b = c = 4;//((a=b)=c)=4;// Error2error C2678: binary '=' :/no operator found which takes a left-hand operand of type 'const Object' /(or there is no acceptable conversion)}
However, this only imposes some restrictions on the customer's code, but is not absolutely secure? No, the customer code can still use the following statement (a = B) = C) = D; this operation:
Const_cast (A = B) = C) = 4;
However, it is more secure than using only non-const references.
To sum up, I think that in Scott Meyers's article 10th "have assignment operators return a reference to * This ", it is best to change it to "have assignment operators return a const reference to * this ". I don't know if other factors are ignored. Let's check it out. Scott Meyers wants to make the custom data type (class) conform to the default behavior of C ++, but the default behavior of C ++ is not necessarily the best. Because the statement (a = B) = C) = 4; is legal, it seems that there is no meaning, and there is no benefit, why not limit it?