运算符重载(操作符重载):可以为运算符增加一些新的功能
常见的运算符有:+ 、-、+=、==、!=、负号、++、—、<< 、>>等

  1. class MyPoint {
  2. friend MyPoint add(MyPoint point1,MyPoint point2); private:
  3. int m_x;
  4. int m_y;
  5. public:
  6. MyPoint(int x,int y):m_x(x),m_y(y){};
  7. int getX(){
  8. return m_x;
  9. }
  10. int getY(){
  11. return m_y;
  12. }
  13. void print(){
  14. std::cout << "( " << m_x << " , " << m_y << " )" << std::endl;
  15. }
  16. };
  17. MyPoint add(MyPoint point1,MyPoint point2){ //能够访问MyPoint里面的所有成员变量
  18. return MyPoint(point1.m_x + point2.m_x,point1.m_y + point2.m_y);
  19. }

我们做两个点相加的时候,使用如下方式

  1. int main(int argc, const char * argv[]) {
  2. MyPoint p1 = MyPoint(1,1);
  3. MyPoint p2 = MyPoint(2,2);
  4. MyPoint point = add(p1,p2);
  5. point.print();
  6. return 0;
  7. }

能否直接使用+号进行两个点相加呢?
image.png
这样是不行的,我们可以使用运算符重载的方式为+号添加这种功能。

  1. class MyPoint {
  2. friend MyPoint operator+(MyPoint point1,MyPoint point2);
  3. private:
  4. int m_x;
  5. int m_y;
  6. public:
  7. MyPoint(int x,int y):m_x(x),m_y(y){};
  8. int getX(){
  9. return m_x;
  10. }
  11. int getY(){
  12. return m_y;
  13. }
  14. void print(){
  15. std::cout << "( " << m_x << " , " << m_y << " )" << std::endl;
  16. }
  17. };
  1. MyPoint operator+(MyPoint point1,MyPoint point2){ //operator为运算符的标识
  2. return MyPoint(point1.m_x + point2.m_x,point1.m_y + point2.m_y);
  3. }

外界使用

  1. int main(int argc, const char * argv[]) {
  2. MyPoint p1 = MyPoint(1,1);
  3. MyPoint p2 = MyPoint(2,2);
  4. MyPoint point = p1 + p2;//这句的本质其实调用了operator+这个函数
  5. // MyPoint point = operator+(p1, p2);
  6. point.print();
  7. return 0;
  8. }

下面两种写法是等价的

  1. MyPoint point = p1 + p2;//这句的本质其实调用了operator+这个函数

上面的写法其实调用了如下函数,只是编译器允许我们直接使用+号运算符。

  1. MyPoint point = operator+(p1, p2);

优化

对operator+进行优化

  1. MyPoint operator+(MyPoint point1,MyPoint point2){ //operator为运算符的标识
  2. return MyPoint(point1.m_x + point2.m_x,point1.m_y + point2.m_y);
  3. }
  1. 参数如果是对象类型,会产生临时对象,调用拷贝构造函数,我们可以使用引用或指针作为参数

    1. MyPoint operator+(MyPoint& point1,MyPoint& point2){ //operator为运算符的标识
    2. return MyPoint(point1.m_x + point2.m_x,point1.m_y + point2.m_y);
    3. }
  2. 参数类型最好是const,这样接收范围就大一些。const可以接受const类型的参数也可以接收非const类型的参数。

    1. MyPoint operator+(const MyPoint& point1,const MyPoint& point2){ //operator为运算符的标识
    2. return MyPoint(point1.m_x + point2.m_x,point1.m_y + point2.m_y);
    3. }

    外界调用

    1. const MyPoint p1 = MyPoint(1,1);
    2. MyPoint p2 = MyPoint(1,1);
    3. MyPoint p3 = p1 + p2;

    重载成员函数

    我们把operator+变成成员函数 ```cpp

    include

class MyPoint{

private: int m_x; int m_y; public:

  1. MyPoint(int x,int y):m_x(x),m_y(y){};
  2. int getX(){
  3. return m_x;
  4. }
  5. int getY(){
  6. return m_y;
  7. }
  8. void print(){
  9. std::cout << "( " << m_x << " , " << m_y << " )" << std::endl;
  10. }
  11. MyPoint operator+(const MyPoint& point){ //operator为运算符的标识
  12. return MyPoint(m_x + point.m_x,m_y + point.m_y);
  13. }

};

外界使用
```cpp
int main(int argc, const char * argv[]) {

        MyPoint p1 = MyPoint(1,1);
        MyPoint p2 = MyPoint(1,1);
        MyPoint p3 = p1 + p2;

    return 0;
}

为了const类型的MyPoint也能调用,我们可以将operator+声明为const函数

MyPoint operator+(const MyPoint& point) const{ //operator为运算符的标识
    return MyPoint(m_x + point.m_x,m_y + point.m_y);
}

运算符重载最好保留运算符原有的特性。比如
截屏2022-06-21 22.46.26.png
a + b是不能被赋值的。
我们的p1 + p2是可以被赋值的

MyPoint p1 = MyPoint(1,1);
MyPoint p2 = MyPoint(1,1);
p1 + p2 = MyPoint(1,1);

改造:如果返回的是一个常量,常量是不可以被赋值的

const MyPoint operator+(const MyPoint& point) const{ //operator为运算符的标识
    return MyPoint(m_x + point.m_x,m_y + point.m_y);
}

截屏2022-06-21 22.51.50.png

仿函数(函数对象)

将一个对象当做一个函数一样来使用。对比普通函数,每次调用函数可以保存对象的一些状态。比如

#include <iostream>
using namespace std;
class MyPoint{

private:
  int  m_x;
  int  m_y;
public:

    MyPoint(int x =0,int y=0):m_x(x),m_y(y){};

    int operator()(int a,int b){
        m_x = a;
        m_y = b;
        return a + b;
    }

    void print(){
        cout << "( " << m_x  << " , " << m_y << " )" << std::endl;
    }

};

外界使用

int main(int argc, const char * argv[]) {

    MyPoint point1;
    int c = point1(1,1);//把对象当做函数来使用,本质还是调用point1.operator()(1, 1)
    cout << c << endl;

    return 0;
}

调用父类运算符重载函数

父类的成员变量通常是私有的,如何在运算符重载里面初始化父类的成员变量

#include <iostream>
using namespace std;
class Animal{

private:
  int  m_age;
public:
    Animal(int age):m_age(age){};

    Animal& operator=(const Animal &animal){
        m_age = animal.m_age;
        return *this;
    }

};

class Cat : public Animal {
private:
  int  m_weight;
public:
    Cat(int weight,int age):m_weight(weight),Animal(age){};

    Cat& operator=(const Cat & cat){
        this->m_weight = cat.m_weight;
        Animal::operator=(cat); //调用父类的operator=初始化父类的成员变量
        return *this;
    }
};

外界使用

int main(int argc, const char * argv[]) {

    Cat cat1(1,1);
    Cat cat2(2,2);
    Cat cat3 = cat2; //调用运算符=号
//    cout << c << endl;

    return 0;
}

练习

+=运算符

     MyPoint& operator+=(const MyPoint& point) {

         this->m_x = this->m_x + point.m_x;
         this->m_y = this->m_y + point.m_y;
         return *this;
     }

==运算符

    bool operator==(const MyPoint& point){

        return (m_x == point.m_x)&& (m_y == point.m_y);
    }

前置++

    MyPoint& operator++(){ //前置++
        m_x++;
        m_y++;
        return *this;
    }

后置++

   const MyPoint operator++(int){ //后置++,编译器为了区分是前置++还是后置++。使用int类型的参数作为区分,固定写法。
        MyPoint old(m_x,m_y);
        m_x++;
        m_y++;
        return old;
    }

输出<<

需要使用全局函数,如果使用成员函数左边必须是类的对象(这里指Mypoint对象)调用。

#include <iostream>
using namespace std;
class MyPoint{
    friend ostream& operator<<(ostream &cout, const MyPoint &point);
private:
  int  m_x;
  int  m_y;
public:

    MyPoint(int x,int y):m_x(x),m_y(y){};
    int getX(){
        return m_x;
    }
    int getY(){
        return m_y;
    }
    void print(){
        cout << "( " << m_x  << " , " << m_y << " )" << std::endl;
    }

};

ostream& operator<<(ostream &cout, const MyPoint &point){
    cout << "( " << point.m_x << "," <<point.m_y << " )";
    return cout;
}

外界使用

    MyPoint p1 = MyPoint(1,1);
    MyPoint p2 = MyPoint(1,1);

    cout << p1 << p2;

输入>>

#include <iostream>
using namespace std;
    friend istream& operator>>(istream &cin,MyPoint &point);
private:
  int  m_x;
  int  m_y;
public:

    MyPoint(int x,int y):m_x(x),m_y(y){};
    int getX(){
        return m_x;
    }
    int getY(){
        return m_y;
    }
    void print(){
        cout << "( " << m_x  << " , " << m_y << " )" << std::endl;
    }

};


istream& operator>>(istream &cin,MyPoint &point){
    cin >> point.m_x;
    cin >> point.m_y;
    return cin;
}

外界使用

cin >> p1;

注意点

有些运算符不可以重载

对象成员访问运算符 . 点

image.png

域运算符

image.png

三目运算符

image.png

sizeof

image.png

有些运算符只能重载为成员函数

赋值运算符:=

下标运算符:[]

函数运算符:()

指针运算符:->