C++基础——运算符重载

1 概述

程序员可以对C++中的+、=等运算符重新定义,以适应不同数据类型的计算。
运算符重载规则:

  • 重载后的运算符不能改变原有运算符的运算规则(加号运算符不能重载为两个数相减)
  • 不能定义原本没有的运算符
    运算符重载语法:
retValue operator[+-*/...](param1, param2) {}

使用operator关键字,后接要重载的运算符,其他的都是和普通函数定义相同。

2 常用运算符重载

2.1 加号运算符重载

#include <iostream>
using namespace std;

class TestClass {
public:
    TestClass(){};
    TestClass(int a, int b):mA(a),mB(b){};

    TestClass operator+(const TestClass& TestClass) const {
        class TestClass temp;
        temp.mA = this->mA + TestClass.mA;
        temp.mB = this->mB + TestClass.mB;
        return temp;
    }
public:
    int mA;
    int mB;
};

// TestClass operator+(const TestClass& testClass1, const TestClass& testClass2) {
//     class TestClass temp;
//     temp.mA = testClass1.mA + testClass2.mA;
//     temp.mB = testClass1.mB + testClass2.mB;
//     return temp;
// }

int main() {
    TestClass testClass1(10,20);
    TestClass testClass2(30,40);
    TestClass testClass3 = testClass1 + testClass2;
    cout << "testClass3.mA = " << testClass3.mA << endl;
    cout << "testClass3.mB = " << testClass3.mB << endl;
    return 0;
}

加法运算符重载,可以实现两个对象相加返回一个对象,而对象的哪些属性参与运算则由软件开发者自己定义。
可以通过全局函数实现,也可以通过成员函数实现。二者的区别是全局函数需要传入两个参数,而成员函数由于this指针的存在只需要传入一个参数。
需要注意的是这里的全局函数实现和成员函数实现不能同时存在,因为调用的时候会产生分歧,不知道应该调用成员函数还是全局函数。

2.2 左移运算符重载

通常输入输出操作都是通过流来实现,像cout、cin函数,就是重载了左移和右移运算符的,通常重写这两个运算符也是为了文件的输入输出操作。

#include <iostream>

using namespace std;

class TestClass {
    friend ostream& operator<<(ostream& out, TestClass& testClass);
public:
    TestClass() {};

    TestClass(int a, int b) : mA(a), mB(b) {};

    void operator<<(ostream &out) const {
        out << "mA = " << this->mA << endl;
        out << "mB = " << this->mB << endl;
    }

private:
    int mA;
    int mB;
};

ostream &operator<<(ostream &out, TestClass &testClass) {
    out << "mA = " << testClass.mA << endl;
    out << "mB = " << testClass.mB << endl;
    return out;
}

int main() {
    TestClass testClass(10, 20);
    testClass << cout;
    cout << testClass << "operator<< ok" << endl;
}

输出

mA = 10
mB = 20
mA = 10
mB = 20
operator<< ok

这两个运算符不适合使用成员函数运算符重载的方式,因为成员函数运算符重载,运算符的左操作数为this指向的对象,所以会导致ostream对象在前,可语义不符。
可以使用全局函数重载左移和右移运算符,用作输入输出。

2.3 自增运算符重载

除了两个操作数的运算符外,还有自增自减这些单操作数的运算符。

#include <iostream>

using namespace std;

class Integer {
public:
    Integer(int num) : m_num(num) {};

    // 前置++
    Integer operator++() {
        m_num++;
        return *this;
    }

    // 后置++
    Integer operator++(int) {
        // 先记录原值,返回原值
        Integer temp = *this;
        m_num++;
        return temp;
    }

public:
    int m_num;
};

ostream &operator<<(ostream &out, Integer integer) {
    out << integer.m_num;
    return out;
}

int main() {
    Integer integer = 10;
    cout << integer++ << endl;
    cout << ++integer << endl;
}

自增运算符分为前置和后置的区别,前置,先增加,后返回;后置,先返回,后增加。在重载运算符的时候也需要遵从此运算法则。

2.5 赋值运算符重载

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

  • 1、默认构造函数
  • 2、默认析构函数
  • 3、默认拷贝构造函数
  • 4、默认赋值运算符
    其中默认赋值运算符,默认对属性的值进行拷贝,如果类中持有其他类对象,则需要该默认的赋值运算符是浅拷贝,浅拷贝在析构时可能出现问题。
#include <iostream>

using namespace std;

class Person {
public:

    Person(int age) {
        m_Age = new int(age);
    }

    Person &operator=(Person &p) {
        if (m_Age != nullptr) {
            delete m_Age;
            m_Age = nullptr;
        }
        //编译器提供的代码是浅拷贝
        //m_Age = p.m_Age;
        //提供深拷贝 解决浅拷贝的问题
        m_Age = new int(*p.m_Age);

        return *this;
    }


    ~Person() {
        if (m_Age != nullptr) {
            delete m_Age;
            m_Age = nullptr;
        }
    }

public:
    int *m_Age;
};

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

int main() {
    test();
}

重载赋值运算符,实现深拷贝。当属性是在堆上开辟时,一定要使用深拷贝,否则可能会导致多次delete释放而导致程序终止。

2.6 关系运算符重载

重载关系运算符,可以对两个自定义类型数据进行比较

#include <iostream>

using namespace std;

class Person {
public:
    Person(string name, int age) {
        this->m_Name = name;
        this->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 test() {
    //int a = 0;
    //int b = 0;

    Person a("孙悟空", 18);
    Person b("孙悟空", 18);

    if (a == b) {
        cout << "a和b相等" << endl;
    } else {
        cout << "a和b不相等" << endl;
    }

    if (a != b) {
        cout << "a和b不相等" << endl;
    } else {
        cout << "a和b相等" << endl;
    }
}


int main() {
    test();
}

2.7 函数调用运算符重载

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

using namespace std;

class MyPrint {
public:
    void operator()(string text) {
        cout << text << endl;
    }

};

void test01() {
    //重载的()操作符 也称为仿函数
    MyPrint myFunc;
    myFunc("hello world");
}


class MyAdd {
public:
    int operator()(int v1, int v2) {
        return v1 + v2;
    }
};

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

    //匿名对象调用  
    cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}

int main() {

    test01();
    test02();

    return 0;
}

使用对象调用重载后的函数调用符函数,就像直接调用函数一样

2.8 重载数组下标运算符

#include <iostream>

using namespace std;

class Array {
public:
    Array(int length = 0);

    ~Array();

public:
    int &operator[](int i);

public:
    int length() const { return m_length; }

    void display() const;

private:
    int m_length;  //数组长度
    int *m_p;  //指向数组内存的指针
};

Array::Array(int length) : m_length(length) {
    if (length == 0) {
        m_p = nullptr;
    } else {
        m_p = new int[length];
    }
}

Array::~Array() {
    delete[] m_p;
}

int &Array::operator[](int i) {
    return m_p[i];
}

void Array::display() const {
    for (int i = 0; i < m_length; i++) {
        if (i == m_length - 1) {
            cout << m_p[i] << endl;
        } else {
            cout << m_p[i] << ", ";
        }
    }
}

int main() {
    int n = 3;
    Array A(n);
    for (int i = 0, len = A.length(); i < len; i++) {
        A[i] = i * 5;
    }
    A.display();
}

可以重载运算符下标运算符,实现数组的随机访问类似功能。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值