C ++ _ operator overload summary, operator overload Summary

Source: Internet
Author: User

C ++ _ operator overload summary, operator overload Summary

What is an operator overload?

The combination of operators and classes produces new meanings.

Why is Operator overload introduced?

Purpose: To implement class polymorphism (polymorphism means that a function name has multiple meanings)

How can I overload operators?

Method: member functions or friend functions of the class (common functions outside the class)

Rules: operators that cannot be overloaded include. And. * And? : And: and sizeof

Usage of friend functions and member functions: Generally, we recommend that you use a member function as the unary operator and a binary operator as the friend function.

1. If an operator needs to modify the state of a class object, a member function is used. If the operator of the left-value operand needs to be implemented (such as =, ++ =, ++)

2. During the operation, you must use youyuan for the mixed operation of counts and objects.

3. In binary operators, when the first operand is not an object, you must use a friend function. Such as input and output operators <and>

The specific rules are as follows:

Operator Recommended
All unary Operators Member Functions
= () []-> Must be a member function
+ =-=/= * = ^ = & =! ==>>=<=, It seems that all the equal signs are here Member Functions
All other binary operators, such as-, + ,*,/ Youyuan Function
<> Must be a friend Function

 

2. Parameters and return values

When the parameter is not changed, it is generally passed by const reference (if the member function is used for overload, the function is also const ).

Determine the return value:

1) if the return value may appear on the left side of the = sign, it can only be used as the left value and a non-const reference is returned.

2) If the return value can only appear on the right side of the = sign, the value can only be used as the right value, and the value of the const type or the const type is returned.

3) if the return value may appear on the left or right of the = sign, the return value must be the left value and a non-const reference is returned.

Example of Operator Overloading:

+ And-Operator Overloading:

Class Point {private: int x; public: Point (int x1) {x = x1;} Point (Point & p) {x = p. x;} const Point operator + (const Point & p); // use the member function to overload the plus sign operator friend const Point operator-(const Point & p1, const Point & p2 ); // use the youyuan function to overload the minus sign operator}; const Point: operator + (const Point & p) {return Point (x + p. x);} Point const operator-(const Point & p1, const Point & p2) {return Point (p1.x-p2.x );}

Call:

Point a (1); Point B (2); a + B; // correct. Call the member function a-B; // correct. Call the member function a + 1; // correct, first call the type conversion function, Turn 1 into an object, then call the member function A-1; // correct, first call the type conversion function, Turn 1 into an object, then call the youyuan function 1 + a; // error. when calling the member function, the first operand must be an object, because the first operand also has the function of calling the member function 1-a; // correct, convert the Data Type first and then call the membership function.

Summary:

1. Because +-is displayed on the Right of =, for example, c = a + B, a right value is returned and a const value can be returned.
2. The following expressions discuss the mixed operators of numbers and objects. In this case, we usually use the youyuan function.

3. binary operator overloading:

Heavy-duty operator function name: operator @ (parameter table)

Implicit call: obj1 + obj2

Explicit call form: obj1.operator + (OBJ obj2) --- member function

Operator + (OBJ obj1, OBJ obj2) --- youyuan Function

During execution, both the implicit call form and the explicit call form call the function operator + ()

++ And -- Operator Overloading:

Class Point {private: int x; public: Point (int x1) {x = x1;} Point operator ++ (); // The member function defines the custom const Point operator ++ (int x); // The suffix can return a value of the const type: friend Point operator -- (Point & p ); // friend meta function definition -- friend const Point operator -- (Point & p, int x); // The suffix can return a value of the const type}; Point :: operator ++ () // ++ obj {x ++; return * this;} const 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} const Point operator -- (Point & p, int x) // obj -- {Point temp = p; p. x --; return temp; // The extension format obj -- when the overload is performed, there is a virtual parameter of the int type, and the original copy status is returned}

Function call:

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>

Summary:

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 obj) // prefix

Operator @ (OBJ obj, int x) // suffix

During execution, both the implicit call form and the explicit call form call the function operator @()

Overload subscript operator []

Class Point {private: int x [5]; public: Point () {for (int I = 0; I <5; I ++) {x [I] = I ;}} int & operator [] (int y) ;}; int & Point: operator [] (int y) {static int t = 0; if (y <5) {return x [y];} else {cout <"subscript "; return t ;}}

Call:

Point a; for (int I = 0; I <10; I ++) {cout <a [I] <endl; // no matter whether the I subscript is out of bounds, every time a [I] is used, the overload of [] will be called} a [0] = 10;

Purpose:

1. The object [x] is similar to the array name [x], which is more suitable

2. You can determine whether the subscript is out of bounds.

Syntax:

Overload method: Only member function Overloading is allowed.

Function Name: operator [] (parameter table)

Parameter table: a parameter with only one parameter. This parameter sets the lower value, usually an integer, but can also be a string (as a subscript ).

Function call: explicit call: Obj [arg]-object [subscript]

Implicit call: obj. operator [] (arg)

Return type:

1. Return function reference + return the actual type of the member (defined by the programmer according to the function body)

2. Because the return value can be left or right, the return value should not be of the const type.

However, to access the const object, there are two versions of the subscript operator overload: Non-const and const. (To be written)

For example: int & Point: operator [] (int y) // Why is the return reference used? The returned value can be a left value or a right value, and the return reference must be used.

Overload operator ()

class Point  {  private:      int x; public:      Point(int x1)    {      x=x1;}      const int operator()(const Point& p);};  const int Point::operator()(const Point& p){    return (x+p.x);}

Call:

Call: Point a (1); Point B (2); cout <a (B );

Purpose of the overload operator:

1. The object () is similar to the function name (x) and is more suitable for use.

Syntax:

Overload method: Only member function Overloading is allowed.

You can continue to reload

Function Name: operator () (parameter table)

Parameter table: the parameters are random, depending on the actual situation.

Function call: explicit call: Obj (x)

Implicit call: obj. operator () (x)

Return type:

1. The actual type of returned Members is random, which is defined by the programmer according to the function body.

2. Because the return value can only be the right value, read-only, the return value should be of the const type.


Overload input/output operators <>

Class Point {private: int x; public: Point (int x1) {x = x1;} friend ostream & operator <(ostream & cout, const Point & p ); // use the friend functions overload <output operator friend istream & operator> (istream & cin, Point & p); // use the friend functions overload> output operators }; ostream & operator <(ostream & cout, const Point & p) {cout <p. x <endl; return cout;} istream & operator> (istream & cin, Point & p) {cin> p. x; return cin ;}
Call: Point a (1); Point B (2); cin> a> B; cout <a <B <endl;

Syntax:

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.