当前位置: 首页 > news >正文

C++运算符重载的学习笔记

加号运算符重载

  • 两种方式
  1. 成员函数重载加号
#include  <iostream>  
using namespace std;  
//加号运算符重载  
 
class Person  
{  
public:  
//    1.成员函数重载加号  
   Person operator+(Person&p)  
   {  
       Person tmp;  
       tmp.m_A = this->m_A + p.m_A;  
       tmp.m_B = this->m_B + p.m_B;  
       return tmp;  
   }  
public:  
   int m_A;  
   int m_B;  
};  
 
void test01()  
{  
   Person p1;  
   p1.m_A = 10;  
   p1.m_B = 10;  
   Person p2;  
   p2.m_A = 10;  
   p2.m_B = 10;  
 
   //成员函数的本质调用  
   Person p3 = p1.operator+(p2);  
   //Person p3 = p1 + p2;  
   cout << "p3.m_A = " << p3.m_A << endl;  
   cout << "p3.m_B = " << p3.m_B << endl;  
 

}    
int main()  
{  
   test01();  
   system("pause");  
   return 0;  
}
  1. 全局函数重载加号
#include  <iostream>  
using namespace std;  
//加号运算符重载  
  
class Person  
{  
public:  
    int m_A;  
    int m_B;  
};  
  
//2.全局函数重载  
Person operator+(Person&p1, Person& p2)  
{  
    Person tmp;  
    tmp.m_A = p1.m_A + p2.m_B;  
    tmp.m_B = p1.m_B + p2.m_B;  
    return tmp;  
}  

void test01()  
{  
    Person p1;  
    p1.m_A = 10;  
    p1.m_B = 10;  
    Person p2;  
    p2.m_A = 10;  
    p2.m_B = 10;  
  

  
  
    //全局函数重载的本质调用  
    Person p3 = operator+(p1, p2);  
  
    //Person p3 = p1 + p2;  
  
    cout << "p3.m_A = " << p3.m_A << endl;  
    cout << "p3.m_B = " << p3.m_B << endl;  
  

}  
  
  
int main()  
{  
    test01();  
    system("pause");  
    return 0;  
}

3.在运算符重载中也可以发生函数重载

#include  <iostream>  
using namespace std;  
//加号运算符重载  
  
class Person  
{  
public:  
    int m_A;  
    int m_B;  
};  
  
//函数重载的版本  
Person operator+(Person& p1, int a)  
{  
    Person tmp;  
    tmp.m_A = p1.m_A + a;  
    tmp.m_B = p1.m_B + a;  
    return tmp;  
}  
void test01()  
{  
    Person p1;  
    p1.m_A = 10;  
    p1.m_B = 10;  
    Person p2;  
    p2.m_A = 10;  
    p2.m_B = 10;   
  
    //Person p3 = p1 + p2;  
  
    //运算符重载,也可以发生函数重载  
    Person p4 = p1 + 100; 
  
    cout << "p4.m_A = " << p4.m_A << endl;  
    cout << "p4.m_B = " << p4.m_B << endl;  
}  
  
  
int main()  
{  
    test01();  
    system("pause");  
    return 0;  
}
  • 由于编译器不知道我们的自定义类型是如何进行相加的,所以我们必须要创建一个全局的或者构造的函数来重载这个加号,以实现我们的目的

左移运算符重载

#include <iostream>
using namespace std;
class Person
{
    friend ostream& operator <<(ostream& cout, Person& p);
public:
    Person(int a, int b)
    {
        m_A = a;
        m_B = b;
    }
private:
    int m_A;
    int m_B;
};
ostream & operator <<(ostream& cout, Person& p)
{
    cout << p.m_A << "  " << p.m_B << endl;
    return cout;
}
void test01()
{
    Person p(10, 10);
//    p.m_A = 10;
//    p.m_B = 10;
//    cout << p.m_A << "  " <<p.m_B << endl;
    cout << p << "  GUET" << endl;
}
int main()
{
    test01();
    return 0;
}

  • 因为要保持cout << 这样的格式,所以在左移运算符重载中我们只用全局函数来进行重载
  • 由于我们要保留链式的特性,那么重载函数的返回值必须是对ostream&这种类型,保证返回的是同一个“东西“,才能在原来的基础上继续输出
  • 由于类中的成员变量为私有,我们必须在类中声明一下重载函数为友元

赋值运算符重载

#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)
    {
        MyInt tmp = *this;
        m_Num--;
        return tmp;
    }
private:
    int m_Num;
};
//写后置的时候会报错
ostream& operator<<(ostream& cout, MyInt myInt)//这里的MyInt我用了引用会怎么样?
{
    cout << myInt.m_Num << endl;
}
void test01()
{
    MyInt myint;
    cout << --(--myint) << endl;
}
void test02()
{
    MyInt myint;
    cout << myint-- << endl;
    cout << myint << endl;
}

int main()
{
    test02();
    //test01();
    return 0;
}

  • 在赋值运算符的重载中要注意前置和后置的区别。
    MyInt& operator--()
    {
        //先--
        m_Num--;
        //再返回
        return *this;
    }
    MyInt operator--(int)
    {
        MyInt tmp = *this;
        m_Num--;
        return tmp;
    }
  1. 返回值类型不一样,一个是返回同一个东西,另一个则是返回–之前的值
  2. 由于函数的重载不能仅靠返回值不同来区分,所以有一个函数的形参部分有个int类型来区分两个函数

原因
因为我们进行前置–,要对类创建的对象的成员变量进行修改,所以我们直接修改它本身然后返回它本身。而后置,我们不能先return否则函数提前结束,所以只能用一个临时变量来存储它被修改之前的值,然后对它本身进行修改再返回那个临时值。
同时,由于临时变量在函数结束时会销毁,那么再用引用会导致非法访问


关系运算符重载

#include <iostream>
using namespace std;
class Person
{
public:
    Person(string name, int age)
    {
        m_Name = name;
        m_Age = age;
    }

    bool operator==(Person& p)
    {
        if (m_Name == p.m_Name && m_Age == p.m_Age)
        {
            return true;
        }
        return false;
    }

    bool operator!=(Person& p)
    {
        if (m_Name == p.m_Name && m_Age == p.m_Age)
        {
            return false;
        }
        return true;
    }


    string m_Name;
    int m_Age;
    //重载 == 号
};
void test01()
{
    Person p1("Tom", 19);

    Person p2("Tom", 18);

    if (p1 == p2)
    {
        cout << "p1 和 p2 是相等的" << endl;
    }
    else
    {
        cout << "p1 和 p2 不相等" << endl;
    }
}
int main()
{
    test01();
    return 0;
}


函数调用运算符重载

#include <iostream>
using namespace std;
//函数调用运算符重载

//打印输出类
class MyPrint
{
public:
    void operator() (string text)
    {
        cout << text << endl;
    }
};
//相加类
class MyAdd
{
public:
    int operator() (int a, int b)
    {
        return a + b;
    }
};
void test01()
{
    MyPrint p;
    p("hello GUET");
}
void test02()
{
    MyAdd add;
    cout << add(100, 200) << endl;
    //匿名函数对象
    cout << MyAdd()(200, 500) << endl;
}
int main()
{
    //test01();
    test02();
    return 0;
}

相关文章:

  • 初阶数据结构习题【6】(3顺序表和链表)—— 206. 反转链表
  • 大模型——基于 DIFY 的自动化数据分析实战
  • 文件魔数与其他特征:揭开文件识别的神秘面纱
  • 深入理解指针与回调函数:从基础到实践
  • 【华为】查看防火墙会话表命令
  • 服务器时间同步
  • 覆盖率记录, 非cross bin
  • Kafka底层结构
  • 使用winlogbeat采集windows日志
  • 《 C++ 点滴漫谈: 二十九 》风格 vs. C++ 风格:类型转换的对决与取舍
  • 解锁智能变革密码:浙江大学2025年DeepSeek行业应用案例集深度解析
  • x 的平方根
  • Qt开发⑪Qt网络+Qt音视频_使用实操
  • HTTP超文本传输协议
  • 排序算法——快速排序
  • 探秘基带算法:从原理到5G时代的通信变革【一】引言
  • Unity 打包后EXE运行出现Field to Load il2cpp的一种情况
  • 《解锁HarmonyOS NEXT高阶玩法:艺术图像识别功能开发全攻略》
  • 打造高清3D虚拟世界|零基础学习Unity HDRP高清渲染管线(第一天)
  • 【JAVA面试题】== 和 equals() 的区别与使用场景
  • 中铁房地产24.7亿元竞得上海松江新城宅地,溢价率20.42%
  • 保利42.41亿元竞得上海杨浦东外滩一地块,成交楼面单价超8万元
  • 教育部、国家发改委联合启动实施教师教育能力提升工程
  • 云南临沧一行贿案金额认定比受贿案多41万,重审时检方变更金额起诉
  • 广州下调个人住房公积金贷款利率
  • 化学家、台湾地区“中研院”原学术副院长陈长谦逝世