Valid tive c ++ terms 10-12 (operator = (heavy return type, self-assignment and deep replication)
I. Heavy Load operator = return type
Operator = is similar.
For: ostream & operator <(ostream & OS, const ClassType & object)
Notes:
1. the first parameter is a reference to the ostream object, which will generate output on the object. The ostream is not a const, because the status of the stream changes when it is written to the stream. this parameter is a reference, because ostream objects cannot be copied (the standard input/output stream classes istream and ostream defined in c ++, both the copy constructor and the value assignment operator function are placed in the private part, and only declarations, not defined ).
2. the second parameter is generally a reference to the class type to be output. This parameter is a reference to avoid copying the real parameter and reduce the copy time. It is set to const, because the output usually does not change this object, setting it to const can be used to output the const object and non-const object.
3. the return type is an ostream reference. Its value is usually the ostream object operated by the output operator. First, because the ostream object cannot be copied, it must be referenced. Second, the reference can be copied once less, improve efficiency. Finally, in order to reflect continuity and achieve continuous output, achieve the effect of using multiple output operators to operate an ostream object. If it is not referenced, when the program returns, a new temporary object is generated, that is, two consecutive < <操作符实际上是针对不同对象的,这就好比cout Overload addition operator, continuous addition cannot return reference
Ii. Solve operator = self-Assignment Problem
When operator = is implemented, it is necessary to consider self-assignment, just as x = y, we do not know whether the values x and y represent the same value (it is more appropriate to say x and y as a pointer ). As follows:
Class bitmap {}; class Widget {public: Widget & operator = (const Widget & rhn); private: bitmap * pb; // defines a pointer pointing to the allocated space}The first assignment function:
Widget& Widget::operator=(const Widget& rhs){ delete pb; pb = new bitmap(*rhs.pb); return this;}The pb of this function clears the point of the pb before use, and accepts a new object, however, when this is equal to the rhs parameter of the function, pb = new bitmap (* rhs. pb); will execute an error because we have put * rhs. pb delete.
Assignment functions for the second edition:
Widget& Widget::operator=(const Widget& rhs){ if(*this == rhs) return *this; delete pb; pb = new bitmap(*rhs.pb) return *this;}This version of the value assignment function is basically acceptable, but it is not safe, because pb is still an uncertain pointer when a new exception occurs.
Version 3:
Widget& Widget::operator=(const Widget& rhn){ bitmap *pOrig = pb; pb = new bitmap(*rhn.pb); delete pOrig; return this;}This function records pb with pOrig at the beginning. When there is no exception in new, we are releasing the original point space of Pb, which improves the security.
Another idea for implementing the value assignment function is the copy and swap technology.
Class Widget {void swap (const Widget & rhs);} Widget & Widget: operator = (const Widget & rhs) {Widget temp (rhs ); // prevent rhs swap (temp); return * this ;}Of course, we can also pass the parameter by value.
Widget & Widget: operator = (const Widget rhs) // pass by value is a copy of the real parameter {swap (temp); return * this ;}The disadvantage of the copy and swap technology is that the clever use of swap loses the clarity of the Code. However, moving the "copy action" to the construction phase of function parameters allows the compiler to sometimes generate efficient code ..
Iii. operator = heavy load function, copy constructor, and deep replication of Constructor
It is easy to understand. When implementing the above functions in the subclass, do not forget to call the corresponding functions of the base class to copy, assign values, and initialize the inherited variables !!