运算符重载

简介: 运算符重载


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

加号运算符重载

作用:实现两个自定义类型的相加的运算
如果现在我们有一个类如下:

class Person
{
    
public:
    Person()
    {
    
        m_a = 10;
        m_b = 30;
    }
public:
    int m_a;
    int m_b;
};

如果我们想实现

    Person p1;
    Person p2;
    Person p3 = p1 + p2;

按普通的加法肯定是不行,这1就要使用加号运算符重载
1.成员函数重载+号

class Person
{
    
public:
    Person()
    {
    
        m_a = 10;
        m_b = 30;
    }
    //成员函数重载 +
    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;
    }

public:
    int m_a;
    int m_b;
}

本质:

      Person p3 = p1 + p2;
本质:Person p3 = p1.operator+(p2);

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 p3 = p1 + p2;
本质:Person p3 = operator+(p1, p2);

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

左移运算符重载

作用:输出自定义类型
比如:
先准备一个类

#include
using namespace std;
class Person
{
    
public:
    Person()
    {
    
        m_a = 10;
        m_b = 10;
    }
    int m_a;
    int m_b;
};

如果我们想实现

Person p;
cout<<p<<endl;

我们就需要利用左移运算符重载

利用全局函数重载左移运算符

ostream& operator<<(ostream &cout, Person p)
{
    
    cout << "m_a= " << p.m_a << "   m_b = " << p.m_b;
    return cout;
}

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

递增运算符重载

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

using namespace std;
class Person
{
    
public:
    Person()
    {
    
        m_Num = 0;
    }
    int m_Num;
};

假如我们要实现:

Person p;
p++;
++p;

我们就需要用到递增运算符重载

前置++

using namespace std;
class Person
{
    
public:
    Person()
    {
    
        m_Num = 0;
    }
    //重载前置++运算符  返回引用为了一直对一个数据进行递增操作
    Person& operator++()
    {
    
        m_Num++;
        return *this;
    }
    int m_Num;
};

后置++:

class Person
{
    
    friend ostream& operator<<(ostream& cout, Person p);
public:
    Person()
    {
    
        m_Num = 0;
    }
    //重载后置++运算符   用占位参数来区分后置++
    Person operator++(int)
    {
    
        Person  temp = *this;
        m_Num++;
        return temp;
    }
    int m_Num;
};

赋值运算符重载

C++编译器会有默认的赋值运算符重载,
赋值运算符operator=,对属性进行值拷贝
但这个默认的赋值运算符operator=,只能对属性进行值拷贝也就是浅拷贝,
如果要进行深拷贝的话,就需要自己写这个赋值运算符重载

#include
using namespace std;
class Person
{
    
public:
    Person(int a)
    {
    
        age = new int(a);
    }
    Person& operator=(Person& p)  //赋值运算符重载
    {
    
        if (age != NULL)
        {
    
            delete age;
            age = NULL;
        }
        age = new int(*p.age);
        return *this;  //返回这个时赋值时可以进行连等操作
    }
    ~Person()    //析构函数,在程序结束后释放空间
    {
    
        if (age != NULL)
        {
    
            delete age;
            age = NULL;
        }
    }
public:
    int* age;

};

关系运算符重载

作用:重载关系运算符,可以让两个自定义类型的对象进行对比操作
比如:
比较两个类的大小关系
要实现这个就需要重载关系运算符

#include
#include
using namespace std;
class Person
{
    
public:
    bool operator==(Person& p)
    {
    
        if (this->age == p.age && this->name == p.name)
            return true;
        else
            return false;
    }
    bool operator!=(Person& p)
    {
    
        if (this->age != p.age || this->name == p.name)
            return true;
        else
            return false;
    }
public:
    string name;
    int age;
};

这样我们就可以进行下面操作了

int main()
{
    
    Person p1;
    p1.age = 18;
    p1.name = "abd";
    Person p2;
    p2.age = 18;
    p2.name = "abd";
    if (p1 == p2)
        cout << "yes" << endl;
    if (p1 != p2)
        cout << "yes" << endl;
}

函数调用运算符重载

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

如果我们要写一个打印类

#include
#include
class Mprint
{
    
public:
    void operator()(string s)
    {
    
        cout << s << endl;
    }
};
int main()
{
    
    Mprint mprintf;
    mprintf("hello word");   //由于使用方法特别像函数调用,所以被称为仿函数
}

如果想实现一个加法类:

#include
#include
class MyAdd
{
    
public:
    int operator()(int a, int b)
    {
    
        return a + b;
    }
};
int main()
{
    
    MyAdd add;
    int ret = add(20, 30);
    cout << ret << endl;
    //匿名对象调用
    cout << MyAdd()(20, 30) << endl;
}
相关文章
|
云安全 人工智能 安全
面对网络威胁,2024年如何做好网络安全工作
为了应对网络安全威胁,企业和个人需要加强网络安全意识,采取有效的安全措施和技术手段,保障网络和数据的安全,维护网络空间的安全稳定。
|
安全 Java Android开发
Android 逆向apk的.so动态库
那么我们上篇文章中提及了安全性问题, Android apk如何加固防止被破解(防止逆向编译),那么本篇文章提及一点,so动态库的安全性与重要性。 首先我们要知道, .so动态库是做什么用的,它不像.smail文件可修改,它是属于汇编语言,如果直接去修改,文件会发生错乱。
2084 0
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16812 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
6天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1209 2
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2288 7