C++运算符重载

文章详细解释了C++中如何通过运算符重载实现自定义数据类型间的操作,包括加法、左移、递增、赋值和关系运算符,以及函数调用运算符的使用实例和注意事项。
摘要由CSDN通过智能技术生成

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

1. 加号运算符重载

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

对于C++内置的数据类型,编译器知道如何运算

示例:

int a = 10;
int b = 10;
int c = a + b;

但是,你自己写的,或者说是你自定义的数据类型的加、减、乘、除、赋值等操作,编译器识别不了这种指令

示例:

class Person
{
public:

    int m_a;
    int m_b;
};

Person p1;
p1.m_a = 10;
p1.m_b = 10;

Person p2;
p2.m_a = 20;
p2.m_b = 20;

person p3 = p1 + p2;  //报错,编译器不能实现这种两个自定义数据类型的相加运算

这种情况下,可以通过自己写成员函数,实现两个对象相加后返回新的对象

示例:

Person PersonAddPerson(Person &p)
{
    Person temp;
    temp.m_a = this->m_a + p.m_a; 
    temp.m_b = this->m_b + p.m_b; 

    return temp;
}

但这种情况下,每个人起得函数名(比如这里我用的PersonAddPerson)都不一样,就很不方便人看懂(不像int 、float一样人人皆知),编译器就给提供了一个统一的函数名:operator+

//通过成员函数重载加号
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;
}

Person p3 = p1.operator+(p2);
还可以简化为:
Person p3 = p1 + p2;
//通过全局函数重载加号
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 p3 = operator+(p1, p2);
还可以简化为:
Person p3 = p1 + p2;

完整代码:

#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 = 10;
    p1.m_b = 11;

    Person p2;
    p2.m_a = 12;
    p2.m_b = 13;

    //成员函数重载的本质调用
    // Person p3 = p1.operator+(p2);

    //全局函数重载的本质调用
    // Person p3 = operator+(p1, p2);

    //二者统一的简化调用
    Person p3 = p1 + p2;

    //运算符重载,也可以发生函数重载
    Person p4 = p3 + 10; 

    cout << "p3.m_a = " << p3.m_a << endl;
    cout << "p3.m_b = " << p3.m_b << endl;
    cout << "p4.m_a = " << p4.m_a << endl;
    cout << "p4.m_b = " << p4.m_b << endl;
}


int main()
{
    test01();

    return 0;
}


输出:
---------------------------------------------------------------------------------
p3.m_a = 22
p3.m_b = 24
p4.m_a = 32
p4.m_b = 34

总结1:对于内置的数据类型的表达式的运算符是不能改变的

总结2:不要滥用运算符重载(命名写的operator+,意味着加法运算,但实际上函数体却是减法运算。可以正常执行,但会误导看你代码的人,不规范)

2. 左移运算符重载

//正常情况
int a = 10;
cout << a << endl;

//报错
Person p;
p.m_a = 10;
p.m_b = 10;
cout << p << endl;
#include<iostream>
using namespace std;


//左移运算符重载

class Person
{
    friend void operator<<(ostream &cout, Person &p);
    friend void test01();  //test01()不能定义成友函数,因为不是符合要求的全局函数
public:
    Person(int a, int b)
    {
        m_a = a;
        m_b = b;
    }
private:
    //利用成员函数重载左移运算符
    //p.operator<<(cout)  对应的简化版本:p << cout,这时cout在p的右侧
    //因此通常不会利用成员函数重载左移运算符
    // void operator<<(cout){}
    int m_a;
    int m_b;
};


//只能利用全局函数重载左移运算符
void operator<<(ostream &cout, Person &p)  //本质:operator<<(cout, p),简化为cout << p
{
    cout << "m_a = " << p.m_a << endl;
    cout << "m_b = " << p.m_b << endl;
}


void test01()
{
    Person p(10, 10);
    // Person p;  //报错:类 "Person" 不存在默认构造函数C/C++(291)
    // p.m_a = 10;
    // p.m_b = 11;

    cout << p;     
}


int main()
{
    test01();

    return 0;
}


输出:
---------------------------------------------------------------------------------
m_a = 10
m_b = 10

总结:重载左移运算符配合友元可以实现输出自定义数据类型

3. 递增运算符重载

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

#include<iostream>
using namespace std;

//重载递增运算符

//自定义整型
class MyInteger
{
    friend ostream& operator<<(ostream& out, MyInteger myint);
public:
    MyInteger()
    {
        m_num = 0;
    }

    //重载前置++运算符,返回引用MyInteger&,而不是MyInteger的目的是为了一直对同一块内存空间进行递增操作
    //如果返回MyInteger的话,链式调用++(++myint)不成立,每次都会重新申请内存空间,只能在原数据基础上+1
    MyInteger& operator++()
    {
        //先进行++运算
        m_num++;

        //再将自身做返回
        return *this;
    }

    //重载后置++运算符
    //int是占位参数,无作用,只是为了区分重载前置++和后置++
    //后置递增返回值而不是引用,因为后置递增需要返回一个临时变量,而引用无法返回临时变量
    MyInteger operator++(int)
    {
        //先记录数据来时候的结果
        MyInteger temp = *this;
        //后递增
        m_num++;
        //再将数据返回
        return temp;
    }

private:
    int m_num;
};


//重载左移运算符
ostream& operator<<(ostream& out, MyInteger myint)
{
    out << myint.m_num;
    return out;
}


void test01()
{
    MyInteger myint;

    cout << ++(++myint) << endl;
    cout << myint << endl;
}


void test02()
{
    MyInteger myint;

    cout << myint++ << endl;
    cout << myint << endl;
}


int main()
{
    test01();
    test02();

    return 0;
}


输出:
---------------------------------------------------------------------------------
2
2
0
1

4. 赋值运算符重载

C++编译器至少给一个类添加4个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝
  4. 赋值运算符operator=,对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时,也会出现深浅拷贝的问题。

示例:

#include<iostream>
using namespace std;

//赋值运算符重载
class Person
{
public:
    
    Person(int age)
    {
        m_age = new int(age);  //堆中的数据需要程序员手动释放
    }

    ~Person()
    {
        if(m_age != NULL)
        {
            delete m_age;
            m_age = NULL;
        }
    }

    //重载赋值运算符
    //如果返回Person,相当于用拷贝构造函数复制了一个新的对象(地址不同);返回Person&,才是对象本身(地址相同)
    Person& operator=(Person &p)
    {
        //编译器默认提供的是浅拷贝
        // m_age = p.m_age;

        //我们需要用深拷贝,先判断是否有属性在堆区,如果有,先释放干净,再深拷贝
        if(m_age != NULL)
        {
            delete m_age;
            m_age = NULL;
        }

        //深拷贝
        m_age = new int(*p.m_age);

        //返回对象本身
        return *this;
    }

    int *m_age;
};


void test01()
{
    Person p1(18);
    Person p2(20);
    Person p3(30);

    p3 = p2 = p1;  //赋值操作,p1、p2、p3指向同一块内存

    cout << "p1的年龄:" << *p1.m_age << endl;
    cout << "p2的年龄:" << *p2.m_age << endl;
    cout << "p3的年龄:" << *p3.m_age << endl;
}

int main()
{
    test01();  
    
    int a = 10;
    int b = 20;
    int c = 30;

    c = b = a;  //从后往前赋值,c=b=a=10

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
    cout << "c = " << c << endl;

    
    // system("read -p '按任意键继续...' var");
    
    return 0;
}


输出:
---------------------------------------------------------------------------------
p1的年龄:18
p2的年龄:18
p3的年龄:18
a = 10
b = 10
c = 10

5. 关系运算符重载

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

示例:

#include<iostream>
using namespace std;

//重载关系运算符

class Person
{
public:
    Person(string name, int age)
    {
        m_name = name;
        m_age = age;
    }

    //重载"=="
    bool operator==(Person &p)
    {
        if (p.m_name == this->m_name && p.m_age == this->m_age)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //重载"!="
    bool operator!=(Person &p)
    {
        if (p.m_name == this->m_name && p.m_age == this->m_age)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    string m_name;
    int m_age;
};

void test01()
{
    Person p1("Tom", 18);
    Person p2("James", 19);

    if (p1 == p2)
    {
        cout << "p1与p2相等" << endl;
    }
    else
    {
        cout << "p1与p2不相等" << endl;
    }

    if (p1 != p2)
    {
        cout << "p1与p2不相等" << endl;
    }
    else
    {
        cout << "p1与p2相等" << endl;
    }

}


int main()
{
    test01();

    return 0;
}


输出:
---------------------------------------------------------------------------------
p1与p2不相等
p1与p2不相等

6. 函数调用运算符重载

  • 函数调用运算符()也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活

示例:

#include<iostream>
using namespace std;

//函数调用运算符重载

class MyPrint
{
public:

    void operator()(string test)
    {
        cout << test << endl;
    }

};

void MyPrint02(string test)
{
    cout << test << endl;
}


void test01()
{
    MyPrint myPrint;

    myPrint("hello world");  //使用起来非常类似于汉航速调用,因此称为仿函数

    MyPrint02("hello world");
}

//仿函数非常灵活,没有固定写法
//加法类
class MyAdd
{
public:

    int operator()(int v1, int v2)
    {
        return v1 + v2;
    }

};

void test02()
{
    MyAdd myAdd;
    int ret = myAdd(10, 10);
    cout << "ret = " << ret << endl;

    //匿名函数对象,当前行执行完立即被释放
    cout << MyAdd()(10, 10) << endl;
}

int main()
{
    test01();
    test02();

    return 0;
}


输出:
---------------------------------------------------------------------------------
hello world
hello world
ret = 20
20
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值