运算符重载

389 阅读2分钟

运算符重载的概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

1.加号运算符重载

作用:实现两个自定义数据类型的相加

可通过成员函数重载,也可以通过全局函数重载

运算符重载,也可以发生函数重载

#include<iostream>
using namespace std;
//加号运算符重载
class Person
{
public:
    //1.通过成员函数重载+号
    /*Person operator+(Person &p)
    {
        Person temp;
        temp.m_A=this->m_A+p.m_A;
        temp.m_B=this->m_B+p.m_B;
        return temp;
    }*/
    int m_A;
    int m_B;
};
//2.通过全局函数重载+号
Person operator+(Person& p1,Person& p2)
{
    Person temp;
    temp.m_A=p1.m_A+p2.m_A;
    temp.m_B=p1.m_B+p2.m_B;
    return temp;
}
Person operator+(Person& p1,int num)
{
    Person temp;
    temp.m_A=p1.m_A+num;
    temp.m_B=p1.m_B+num;
    return temp;
}
void test01()
{
    Person p1;
    p1.m_A=1;
    p1.m_B=2;
    Person p2;
    p2.m_A=3;
    p2.m_B=4;
​
    Person p3=p1+p2;
    //成员函数重载本质调用
    //Person p3=p1.operator+(p2);
    //全局函数重载本质调用
    //Person p3=operator+(p1,p2);
    cout<<"p3.m_A= "<<p3.m_A<<endl;
    cout<<"p3.m_B= "<<p3.m_B<<endl;
​
    //运算符重载,也可以发生函数重载
    Person p4=p1+10;//Person+int
    cout<<"p4.m_A= "<<p4.m_A<<endl;
    cout<<"p4.m_B= "<<p4.m_B<<endl;
}
int main()
{
    test01();
​
    return 0;
}

注意:对于内置的数据类型的表达式的运算符是不可能改变的;不要滥用运算符重载

2.左移运算符重载

作用:可以输出自定义数据类型

注意:只能通过全局函数重载,cout是标准输出流(ostream)对象

#include<iostream>
using namespace std;
class Person
{
public:
    //通常不会利用成员函数重载<<运算符,因为无法实现cout在左侧
    int m_A;
    int m_B;
};
//只能利用全局函数重载 左移运算符
ostream& operator<<(ostream & cout,Person p)//本质 operator<<(cout,p)  简化 cout<<p
{
    cout<<"m_A= "<<p.m_A<<" m_B= "<<p.m_B;
    return cout;
}
void test01()
{
    Person p;
    p.m_A=10;
    p.m_B=10;
    cout<<p<<endl;
}
int main()
{
    test01();
​
    return 0;
}

3.递增运算符重载

作用:通过重载递增运算符,实现自己的整形数据

注:前置递增返回引用,后置递增返回值

#include<iostream>
using namespace std;
//重载递增运算符
class MyInt
{
    friend ostream& operator<<(ostream& cout,MyInt myint);
public:
    MyInt(){
        m_Num=0;
    }
    //重载前置++运算符 前置递增要返回引用
    MyInt& operator++()
    {
        m_Num++;//先进行++运算
        return *this;//再将自身做返回
    }
    //重载后置++运算符 后置递增要返回值
    //MyInt operator++(int) int 代表占位参数,可用于区分前置和后置递增
    MyInt operator++(int)
    {
        MyInt temp=*this;//先记录当时的结果
        m_Num++;//然后递增
        return temp;//最后将之前记录的结果做返回
    }
​
private:
    int m_Num;
};
//先重载<<
ostream& operator<<(ostream& cout,MyInt myint){
    cout<<myint.m_Num<<endl;
    return cout;
}
void test01()
{
    MyInt myint;
    cout<<++(++myint)<<endl;//2
    cout<<myint<<endl;//2
}
void test02()
{
    MyInt myint2;
    cout<<myint2++<<endl;//0
    cout<<myint2<<endl;//1
}
int main()
{
    //test01();
    test02();
    return 0;
}

4.赋值运算符重载

C++编译器会给一个类添加这么一个函数:

赋值运算符operator=,对属性进行值拷贝。如果有类的成员变量开辟空间在堆区,并且有对象的赋值操作(如:p2=p1),那么可能会引发浅拷贝的问题,即堆区内存重复释放,程序崩溃!这时我们就需要用到深拷贝来解决,即可以重载赋值运算符

(有关深浅拷贝的问题,如有需要可以参考我的另一篇博客:juejin.cn/post/705222…)

#include<iostream>
using namespace std;
class Person
{
public:
    int* m_Age;
    Person(int age){
        m_Age=new int(age);
    }
    ~Person(){
        if(m_Age!=nullptr){
            delete m_Age;
            m_Age=nullptr;
        }
    }
    //重载=
    Person& operator=(Person& p){
        //m_Age=p.m_Age 这是编译器提供的浅拷贝
        //应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
        if(m_Age!=nullptr){
            delete m_Age;
            m_Age=nullptr;
        }
        //深拷贝
        m_Age=new int(*p.m_Age);
​
        return *this;
    }
};
void test01(){
    Person p1(18);
    Person p2(20);
    Person p3(30);
    cout<<"p1: "<<*p1.m_Age<<endl;//18
    cout<<"p2: "<<*p2.m_Age<<endl;//20
    cout<<"p3: "<<*p3.m_Age<<endl;//30
    p3=p2=p1;
    cout<<"p1: "<<*p1.m_Age<<endl;//18
    cout<<"p2: "<<*p2.m_Age<<endl;//18
    cout<<"p3: "<<*p3.m_Age<<endl;//18
}
int main()
{
    test01();
    return 0;
}
​
​

5.关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
    Person(string name,int age){
        m_Name=name;
        m_Age=age;
    }
    //重载 ==
    bool operator==(Person& p)
    {
        if(this->m_Name==p.m_Name&&this->m_Age==p.m_Age){
            return true;
        }
        else{
            return false;
        }
​
    }
    string m_Name;
    int m_Age;
};
void test01(){
    Person p1("Tom",18);
    Person p2("Tom",19);
    if(p1==p2){
        cout<<"p1和p1是相等的"<<endl;
    }
    else{
        cout<<"p1和p1是不相等的"<<endl;
    }
}
int main()
{
    test01();
    return 0;
}

6.函数调用运算符重载

  • 函数调用运算符()也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活
#include<iostream>
using namespace std;
class MyPrint
{
public:
    //重载函数调用运算符
    void operator()(string test){
        cout<<test<<endl;
    }
};
void MyPrint2(string test){
    cout<<test<<endl;
}
void test01()
{
    MyPrint myprint;
    myprint("hello world!");//使用重载之后的()
    MyPrint2("hello world!");//正常的函数调用
}
int main()
{
    test01();
    return 0;
}

参考链接

www.bilibili.com/video/BV1et…