【C++】运算符重载

目录

运算符重载

加 号 (+) 运 算 符 重 载

        成员函数重载

        全局函数重载

左 移 (<<) 运 算 符

 递 增 (++) 运 算 符 重 载

        前置递增

        后置递增

赋 值 (=) 运 算 符 重 载

关 系(==、!=)运 算 符 重 载

函 数 调 用 运 算 符 重 载


运算符重载

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

加 号 (+) 运 算 符 重 载

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

成员函数重载

在成员函数内对运算符进行重载,重载 operator+ 写在公共权限下。

#include<iostream>
#include<string>
using namespace std;

// 成员函数重载 +
class Person{
public:
    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;
};

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

    Person p3 = p1 + p2;

    cout << "p3 m_A:" << p3.m_A << endl;
    cout << "p3 m_B:" << p3.m_B << endl;
}

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

Person p3 = p1 + p2;

本质:

Person p3 = p1.operator+(p2);

不用用 operator+ 用别的可不可以呢?

 答案是不可以

全局函数重载

#include<iostream>
#include<string>
using namespace std;
// 成员函数重载 +
class Person{
public:
    int m_A;
    int m_B;
};
Person operator+(Person& p1, Person& p2) {
    Person temp;
    temp.m_A = p1.m_A + p2.m_A;
    temp.m_B = p2.m_B + p2.m_B;
    return temp;
}
void test(){
    Person p1;
    p1.m_A = 10;
    p1.m_B = 10;
    Person p2;
    p2.m_A = 20;
    p2.m_B = 20;

    Person p3 = p1 + p2;

    cout << "p3 m_A:" << p3.m_A << endl;
    cout << "p3 m_B:" << p3.m_B << endl;
}

int main(){
    test();
    system("pause");
    return 0;
}
Person p3 = p1 + p2;

本质:

Person p3 = operator+(p1, p2);

函数重载 + 也可以是不同类型

左 移 (<<) 运 算 符

让 cout 输出对象 p

将 operator<< 写在成员函数

调用的时候就要 operator<<(cout) 简化: p<<cout 

无法实现 cout 在左侧

所以不会利用成员函数重载 << 运算符,

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

#include<iostream>
using namespace std;
// <<
class Person {
public:
    int m_A;
    int m_B;
};
void operator<<(ostream& cout, Person& p) {
    cout << p.m_A << endl << p.m_B << endl;
}
void test01()
{
    Person p;
    p.m_A = 10;
    p.m_B = 10;

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

 cout 类型 :ostream

本质

operator<<(cout,p);

简化

cout << p;

链式编程思想:

 需要对 operator<< 重载进行返回 cout 

ostream& operator<<(ostream& cout, Person& p) 
{
    cout << p.m_A << endl << p.m_B << endl;
    return cout;
}

 最后全局函数重载左移运算符需要访问类中的私有成员需要运用友元技术

#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 << endl << p.m_B << endl;
    return cout;
}
void test01()
{
    Person p(10, 20);
    cout << p << endl;
}
int main() {
    test01();
    system("pause");
    return 0;
}

 递 增 (++) 运 算 符 重 载

前置递增

#include<iostream>
using namespace std;

class MyInteger {
    friend ostream& operator<<(ostream& cout, MyInteger& myint);
public:
    MyInteger() {
        m_Num = 0;
    }
    // 重载 ++ 前置
    MyInteger& operator++() 
    {
        // 进行 ++ 运算
        m_Num++;
        // 在返回自身做返回
        return *this;
    }
private:
    int m_Num;
};

ostream& operator<<(ostream& cout, MyInteger& myint) {
    cout << myint.m_Num << endl;
    return cout;
}
void test() {
    MyInteger myint;
    cout << myint;
    cout << ++myint;
}
int main() {
    test();
    system("pause");
    return 0;
}
MyInteger& operator++()
{
    // 进行 ++ 运算
    m_Num++;
    // 在返回自身做返回
    return *this;
}

返回类型为引用,是为了防止返回的数据是一个新的数据而不是自身,

为了一直对一个数据进行操作

如果返回类型不写成引用则对象只能做一次++

后置递增

当前置 ++ 和 后置 ++ 同时重载时,这时重载的成员函会发生二义性

 可以写占位参数来重载,但只能写 int 来占位

#include<iostream>
using namespace std;
class MyInteger
{
    friend ostream& operator<<(ostream& cout, MyInteger& myint);
public:
    MyInteger()
    {
        m_Num = 0;
    }
    // 前置递增
    MyInteger& operator++()
    {
        m_Num++;
        return *this;
    }
    // 后置递增 
    MyInteger& operator++(int)
    {
        // 记录结果
        // 堆上开辟空间进行返回
        MyInteger* temp = new MyInteger;
        // 递增
        m_Num++;
        // 返回记录值
        return *temp;
    }
private:
    int m_Num;
};

ostream& operator<<(ostream& cout, MyInteger& myint)
{
    cout << myint.m_Num;
    return cout;
}

void test1()
{
    MyInteger myint1;
    cout << myint1 << endl;
    cout << myint1++ << endl;
    cout << myint1 << endl;
}

void test2()
{
    MyInteger myint2;
    cout << myint2 << endl;
    cout << ++myint2 << endl;
}
int main()
{
    test1();
    system("pause");
    return 0;
}

 后置递增不能返回引用,局部变量会在函数结束时被销毁,但可以通过 new 在堆区创建全局变量就可以返回引用

#include<iostream>
using namespace std;
class MyInteger
{
    friend ostream& operator<<(ostream& cout, MyInteger myint);
public:
    MyInteger()
    {
        m_Num = 0;
    }
    // 前置递增
    MyInteger& operator++()
    {
        m_Num++;
        return *this;
    }
    // 后置递增 
    MyInteger operator++(int)
    {
        // 记录值
        MyInteger temp = *this;
        // 递增
        m_Num++;
        // 返回记录值
        return temp;
    }
private:
    int m_Num;
};

ostream& operator<<(ostream& cout, MyInteger myint)// &
{
    cout << myint.m_Num;
    return cout;
}

void test1()
{
    MyInteger myint1;
    cout << myint1 << endl;
    cout << myint1++ << endl;
    cout << myint1 << endl;
}

void test2()
{
    MyInteger myint2;
    cout << myint2 << endl;
    cout << ++myint2 << endl;
}
int main()
{
    test1();
    system("pause");
    return 0;
}

赋 值 (=) 运 算 符 重 载

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;
        }
    }
    // 重载 赋值=
    void operator=(Person& p)
    {
        // 先判断是否有属性在堆区
        // 有先释放
        // 再深拷贝
        if (m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
        m_Age = new int(*p.m_Age);
    }
    int* m_Age;
};

void test1()
{
    Person p1(18);
    Person p2(20);
    p2 = p1; // 赋值 
    cout << "p1 的年龄:" << *p1.m_Age << endl;
    cout << "p2 的年龄:" << *p2.m_Age << endl;
}

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

这里重载=函数返回值为 void ,C++中允许连续赋值 

int a = 10;
int b = 20;
int c = 30;
a = b = c;

当上面提供的重载不能进行连续赋值,链式无法进行

所以需要进行改进

#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& operator=(Person& p)
    {
        // 先判断是否有属性在堆区
        // 有先释放
        // 再深拷贝
        if (m_Age != NULL)
        {
            delete m_Age;
            m_Age = NULL;
        }
        m_Age = new int(*p.m_Age);
        // 返回对象本体
        return *this;
    }
    int* m_Age;
};

void test1()
{
    Person p1(18);
    Person p2(20);
    Person p3(30);
    p2 = p1 = p3; // 赋值 
    cout << "p1 的年龄:" << *p1.m_Age << endl;
    cout << "p2 的年龄:" << *p2.m_Age << endl;
    cout << "p3 的年龄:" << *p3.m_Age << endl;
}

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

关 系 (==、!=) 运 算 符 重 载

作用:可以让两个自定义类型对象进行对比操作

#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;
        }
    }
    bool operator!=(Person& p)
    {
        if ((this->m_Name == p.m_Name) && (this->m_Age == p.m_Age))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    string m_Name;
    int m_Age;
};

void test1()
{
    Person p1("李华", 18);
    Person p2("李华1", 18);
    
    // 类的比较
    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()
{
    test1();
    system("pause");
    return 0;
}

 函数调用运算符重载

  • 函数调用运算符  ( )  可以重载
  • 重载后使用的方式像函数的调用,故称为 仿 函 数
  • 仿函数没有固定的写法,非常灵活
#include<iostream>
#include<string>
using namespace std;

// 打印输出类
class MyPrint
{
public:
    void operator()(string text)
    {
        cout << text << endl;
    }
};

// 自定义函数
void MyPrint2(string text)
{
    cout << text << endl;
}


void test()
{
    MyPrint myPrint;
    // 仿函数
    myPrint("hello world");
    // 自定义函数
    MyPrint2("hello world");
}

// 相加类
class MyAdd
{
public:
    int operator()(int num1, int num2)
    {
        return (num1 + num2);
    }
};

void test2()
{
    MyAdd add;
    cout << add(10, 20) << endl;

    // 匿名函数对象
    cout << MyAdd()(20, 20) << endl;
}
int main()
{
    test();
    test2();
    system("pause");
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值