C ++ operator overloading (easy to understand)

Operator overloading, the existing operator is redefined, giving it another function, to accommodate different data types.

You can redefine or override most of the C ++ built-in operators. For example, +, -, *, /,

++, -, >>, << and so on, so that you can use to customize the type of operator.

The basic format of operator overloading

Operator is overloaded with special function name, the function name is a key operator and

Thereafter the operator to override symbols. As with other functions, operator overloading a

A return type and parameter list.

Point operator+(const Point &);

Operator overloading in two ways: one is (member functions as a function of operator overloading class) class overrides the other is (a friend function as a function of operator overloading and the like) outside the class overloads

Within the class overrides

#include <iostream>
using namespace std;

class Point{
public:
    Point(){};
    Point (int x, int y): x(x),y(y) {};
    Point operator+(const Point &a){ //类内重载,运算符重载函数作为类的成员函数
        Point ret;
        ret.x = this->x + a.x;
        ret.y = this->y + a.y;
        return ret;
    }
    int x,y;
};

int main() {
    Point a(2,4),b(5,3);
    Point c = a + b;
    cout<< "x :" << c.x << endl;
    cout<<"y :" << c.y << endl;
}

When the above code is compiled and executed, it produces the following results:

x : 7

and 7

Overload operator overloading is the class, as a function of operator overloading member function, the above code as an example a + b + corresponds to the object call a method and pass parameters b Object

Heavy-duty class outside

#include <iostream>
using namespace std;

class Point{
public:
    Point(){};
    Point (int x, int y): x(x),y(y) {};
    friend Point operator+(const Point &, const Point &);
    int x,y;
};

Point operator+(const Point &a,const Point &b){//类外重载,运算符重载函数作为类的友元函数
    Point ret;
    ret.x = a.x + b.x;
    ret.y = a.y + b.y;
    return ret;
}

int main() {
     Point a(2,4),b(5,3);
    Point c = a + b;
    cout<< "x :" << c.x << endl;
    cout<<"y :" << c.y << endl;
}

When the above code is compiled and executed, it will produce the same results as above

Examples of various operator overloading

The following code example demonstrates overload various operators, several basic presentation operator overloading.

Insertion Operator >> and << extraction operator overloading

To extract the operator overloading <<, for example, coutit is the ostreamobject class. ostreamClass and coutare in the header file <iostream>declaration. ostreamClass will be <<overloaded as a member function.

Here we override <<use coutoutput a target

#include <iostream>
using namespace std;

class Point{
public:
    Point(){};
    Point (int x, int y): x(x),y(y) {};
    friend Point operator+(const Point &, const Point &);
    friend ostream &operator<<(ostream &out , const Point &a);
private:
    int x,y;
};

Point operator+(const Point &a,const Point &b){
    Point ret;
    ret.x = a.x + b.x;
    ret.y = a.y + b.y;
    return ret;
}

ostream &operator<<(ostream &out , const Point &a){
    out << "<Point>( " << a.x << ", " << a.y << ")";
    return out;
}

int main() {
    Point a(2,4),b(5,3);
    Point c = a + b;
    cout << c<< endl;
}

When the above code is compiled and executed, it produces the following results:

< Point>( 7, 7)

Note: overloading <<, the class is outside overloaded, it is customary that people are using cin>>and cout<<, and have to use a friend function to overload operators, if overloaded member function to occur c<<cout;this unnatural code.

Also someone should ostream &operator<<(ostream &out , const Point &a)function wondering, first overloaded <<, the return value type is ostream&, the first parameter is ostream&. In other words, the expression cout<<cof the return value is still cout, it cout<<c<<endl;can be established

Operator overloading front and rear ++ ++ operator overloading

 #include <iostream>
using namespace std;

class Point{
public:
    Point(){};
    Point (int x, int y): x(x),y(y) {};
    friend Point operator+(const Point &, const Point &);
    friend ostream &operator<<(ostream &out , const Point &a);
    Point& operator++(){ //前置运算符,需要引用返回,不需要参数。返回自增后的值,且返回的是一个左值 
        x++;
        y++;
        return *this;
    }

    const Point operator++(int){//后置++,不需要引用返回,需要参数区分。返回自增前的值,且返回的是一个右值
        Point temp(x,y);
        x++;
        y++;
        return temp;
    }
private:
    int x,y;
};

Point operator+(const Point &a,const Point &b){
    Point ret;
    ret.x = a.x + b.x;
    ret.y = a.y + b.y;
    return ret;
}

ostream &operator<<(ostream &out , const Point &a){
    out << "<Point>(" << a.x << " , " << a.y << ")";
    return out;
}


int main() {
    Point a(2,4),b(5,3);
    Point c = a + b;
    cout << c << endl;
    c++;
    cout << c << endl;
    ++c;
    cout << c << endl;
}

When the above code is compiled and executed, it produces the following results:

(7 , 7)
< Point>(8 , 8)
< Point>(9 , 9)

1> is a difference between front and rear operators, need to add the parameter "int" in the rear operator overloading functions, although the type herein except to show the difference does not represent any real meaning;

2> returns the front reference variable, rear return is constant. So ++++ c legitimate, and c ++++ not legitimate;

3> c ++++ Why not let it be legal? If you want to achieve c ++++ legitimate, you must return to the set of variables or variable references. c ++ c value is to return again +1, it is impossible to return to c, it must first establish a local variable to hold the initial value c, then return to local variables (local variables do not allow returns a reference), but returns a local variable after that, if re-attached at once ++ operator, is involved in computing the value of a local variable, so in this case c ++++ fact equivalent to c ++, there would be no meaning.

Guess you like

Origin www.cnblogs.com/liuchenxu123/p/12538623.html