"What is an operator overload" and "why is an operator overload introduced ?" These two questions are not mentioned here. Baidu will be okay at once.
The following describes some of the more error-prone reloads,
First, let's take a look at how templates declare heavy loads.
Below is a piece of source code in the iterator template. Let's take a look at its format:
Iterator source code
typedef _Vector_iterator<_Ty, _Alloc> _Myt;reference operator*() const{// return designated objectreturn ((reference)**(_Mybase *)this);}pointer operator->() const{// return pointer to class objectreturn (&**this);}_Myt& operator++(){// preincrement++(*(_Mybase *)this);return (*this);}_Myt operator++(int){// postincrement_Myt _Tmp = *this;++*this;return (_Tmp);}_Myt& operator--(){// predecrement--(*(_Mybase *)this);return (*this);}_Myt operator--(int){// postdecrement_Myt _Tmp = *this;--*this;return (_Tmp);}_Myt& operator+=(difference_type _Off){// increment by integer(*(_Mybase *)this) += _Off;return (*this);}_Myt operator+(difference_type _Off) const{// return this + integer_Myt _Tmp = *this;return (_Tmp += _Off);}_Myt& operator-=(difference_type _Off){// decrement by integerreturn (*this += -_Off);}_Myt operator-(difference_type _Off) const{// return this - integer_Myt _Tmp = *this;return (_Tmp -= _Off);}difference_type operator-(const _Mybase& _Right) const{// return difference of iteratorsreturn (*(_Mybase *)this - _Right);}reference operator[](difference_type _Off) const{// subscriptreturn (*(*this + _Off));}
Based on the source code example above, we can easily see:
Prefix ++ (--) and suffix ++ (--)
1. A ++
Function return: temp (temporary variable)
Whether the function returns the const type: the returned result is a temporary variable after the copy operation. It cannot appear on the left of the equal sign (the temporary variable cannot be left). The function can only return the right value, returns a value of the const type.
++
Function return: * this;
Whether the function returns the const type: if the original state is returned, the return value can be the left value. That is, if the function result can be the left value, a non-const type value is returned.
2. The prefix and suffix cannot be distinguished only from the function name (operator ++) and can only be distinguished by parameters. Here, a virtual parameter int X and X can be any integer.
3. Single Object Operator Overloading:
Heavy-duty operator function name: Operator @ (parameter table)
Implicit call: obj1 @ or @ obj1
Explicit call form:
Member functions:
Obj1.operator @ () // prefix
Obj1.operator @ (0) // suffix
Youyuan function:
Operator @ (OBJ) // prefix
Operator @ (OBJ, int X) // suffix
During execution, both the implicit call form and the explicit call form call the function operator @()
The following is an example:
# Include <iostream> using namespace STD; Class Point {PRIVATE: int X; public: Point (INT X1) {x = x1;} Point & operator ++ (); // member function-defined custom point operator ++ (int x); // The suffix can return a value of the const type // point & operator --(); // member function-defined auto-increment // point operator -- (int x); // The suffix can return a value of the const type: Friend point & operator -- (point & P ); // friend meta function definition -- friend point operator -- (point & P, int X); // The suffix can return a value of the const type: Friend ostream & operator <(ostream & out, const point & P) {out <p. x; Return out ;}; point & point: Operator ++ () // + + OBJ {x ++; return * This ;} point :: operator ++ (int x) // OBJ ++ {point temp = * this; this-> X ++; return temp;} Point & operator -- (point & P) // -- OBJ {P. X --; return P; // when the prefix form (-- OBJ) is overloaded, there is no virtual parameter, and * This or its own reference is returned through reference, that is, return the value after the change} point operator -- (point & P, int X) // OBJ -- {point temp = P; p. X --; return temp; // The extension format OBJ -- when reloading, there is a virtual parameter of the int type, and the original state copy is returned} int main () {point A (1 ); point B (2); a ++; // implicitly call the member function operator ++ (0), suffix expression ++; // implicitly call the member function operator ++ (), prefix expression B --; // implicitly call the member function operator -- (0), suffix expression -- B; // call the operator -- (), prefix expression cout <. operator ++ (2); // explicitly call the member function operator ++ (2), suffix expression cout <. operator ++ (); // explicitly call the member function operator ++ (), prefix expression cout <operator -- (B, 2 ); // explicitly call the operator -- (2), suffix expression cout <operator -- (B); // explicitly call the operator --(), prefix expression </PRE> return 0 ;}
Overload input and output operators <> (this is also emphasized)
Overload method: You can only use the youyuan function to overload and use three references &
Function Name:
Output stream: Operator <(parameter table)
Input stream: Operator> (parameter table)
Parameter table: fixed (error-prone). Both parameters are referenced &
Output stream: It must be two parameters: ostream and Object
The first operand, cout, is defined in the object iostream and is a reference to the object of the standard class ostream.
For example, ostream & cout, const point & P
Input stream: It must be two parameters: ostream and Object
The first operand is Cin, which is defined in the object iostream. It is actually a reference to an object of the standard class type istream.
For example, instream & Cin, const point & P
Function call:
Output stream: explicit call: cout <object
Implicit call: Operator <(cout, object)
Input stream: explicit call: CIN> Object
Implicit call: Operator> (CIN, object)
Return type: fixed return type + reference using return function (continuous output is supported)
Output stream: returns ostream &
For example, ostream & operator <(ostream & cout, const point & P)
Input stream: Return Value: istream &
For example, istream & operator> (istream & Cin, point & P)
Note: Why must I use a friend function to overload input and output operators?
Because the member function requires an object to be called, the first parameter must be a class object, but the first parameter <and> is the object reference of the stream. Therefore, member functions cannot be used.
The sample code is as follows:
class ABC{int data ;public:ABC(int d):data(90){}friend ostream&operator<<(ostream&out,const ABC &o) ;friend istream& operator >>(istream & in, ABC &i);};ostream &operator<<(ostream&out,const ABC &o){out<<o.data;return out ;}istream &operator>>(istream& in, ABC &i){in>>i.data ;return in;}
Part content reference: http://blog.csdn.net/insistgogo/article/details/6626952