当前位置: 代码网 > it编程>编程语言>C/C++ > C++类中的运算符重载过程

C++类中的运算符重载过程

2024年11月14日 C/C++ 我要评论
为什么要实现运算符重载?在 c++ 中,运算符最初是为内置类型(如int、double等)定义操作方式。当定义一个新的类(自定义类型)时,编译器并不清楚如何对这个新类型的成员变量应用运算符。通过运算符

为什么要实现运算符重载?

在 c++ 中,运算符最初是为内置类型(如intdouble等)定义操作方式。当定义一个新的类(自定义类型)时,编译器并不清楚如何对这个新类型的成员变量应用运算符。

通过运算符重载,程序员可以明确地告诉编译器对于该类的对象,运算符(如+-*等)应该如何操作其成员变量,从而使自定义类型能够像内置类型一样自然地使用这些运算符。

1.加法运算符重载

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

在类内实现加法运算符重载

#include <iostream>
using namespace std;
class person {
    int age;
    int money;

public:
    person() : money(0), age(0) {}
    person(int val, int val2) : age(val), money(val2) {}
    person(const person& other) {
        this->age = other.age;
        this->money = other.money;
    }
    // 类内实现+重载 本质:person p3=p1.operator+(p2)
    person operator+(const person& other) {
        person p;
        p.age = this->age + other.age;
        p.money = this->money + other.money;
        return p; // 不以引用返回是因为,在执行完这个函数后,p会被销毁,以值传递会调用拷贝构造
    }
    void print() { cout << age << " " << money << endl; }
};

int main() {
    person a(18, 50);    // 括号法
    person b = {12, 50}; // 隐式转换法
    person c = a + b;
    c.print();
    return 0;
}

实现加法运算符重载的operator+函数来完成的,传入的参数为const person& other的原因有以下的两点:

  • 1.以引用的方式传递是为了防止调用拷贝构造
  • 2.加const修饰是为了防止修改实参

在函数中声明了一个person的对象p,返回值是以值的形式返回,因为值返回会调用拷贝构造,如果是以引用的方式传递,当这个函数结束时,对象p就会被销毁掉。

在类外实现加法运算符重载

#include <iostream>
using namespace std;
class person {
    int age;
    int money;
    friend person operator+(const person& other1, const person& other2);

public:
    person() : money(0), age(0) {}
    person(int val, int val2) : age(val), money(val2) {}
    person(const person& other) {
        this->age = other.age;
        this->money = other.money;
    }
    void print() { cout << age << " " << money << endl; }
};
// 类外实现+重载 本质:person p3=operator+(p1,p2)
person operator+(const person& other1, const person& other2) {
    person p;
    p.age = other1.age + other2.age;
    p.money = other1.money + other2.money;
    return p;
}
int main() {
    person a(18, 50);    // 括号法
    person b = {12, 50}; // 隐式转换法
    person c = a + b;
    c.print();
    return 0;
}

在类外实现加法运算符重载,相当于类外函数访问类内的成员变量,所以要将这个函数在类中声明为友元函数(friend)。其他的跟在类内实现加法运算符重载是一样的。

本质:在类内实现加法运算符重载的本质是:person p3=p1.operator(p2)

在类外实现加法运算符重载的本质是:person p3=operator+(p1,p2)

因为一个是通过对象去调用这个函数,它本身也算是一个参数,所以只需要传人一个参数。而在类外,则是相当于直接调用这个函数,所以传入的参数是两个。

无论是再类内还是在类外实现加法运算符重载,最后调用的方式都是+。

2.左移运算符重载

作用:可以输出自定义数据类型

要在类外实现,因为在类内的话,调用格式为:对象.operator();无法达到cout<<

#include <iostream>
using namespace std;
class person {
    int age;
    int money;

public:
    friend ostream& operator<<(ostream& o, const person& p);
    person() : age(0), money(0) {}
    person(int val, int val2) : age(val), money(val2) {}
    person(const person& other) {
        cout << "调用拷贝构造";
        this->age = other.age;
        this->money = other.money;
    }
    // 一般不在函数内进行左移重载
    /*void operator<<(ostream& cout,const person p) {

    }*/
};
// 类外实现左移运算符重载
ostream& operator<<(ostream& o, const person& p) {
    o << p.age << " " << p.money << endl;
    return o;
}
int main() {
    person a(5, 12), b;
    cout << a << b << endl;
    return 0;
}

因为是在类外实现的,所以在类内要将其设置为友元函数。参数为两个,一个为ostream类型的o,另外一个为引用类型的对象。

返回值为引用类型的ostream类型的ostream,因为如果想要进行连续的输出,就必须让前一个的结果作为第二个左移运算符的第一个参数,如下图:

在o<<p.age的返回值为o,然后它与后面就变成了o<<" ",接着又调用这个运算符,这样才能完成连续的输出。

3.递增运算符重载

递增分为两种一种是前++,一种是后++。前++是在使用之前就让这个数加1,返回的是加1之后的结果。而后++是先返回这个数再进行加一操作。

前++返回的是引用,后++返回的是值。

#include <iostream>
using namespace std;
class person {
    int age;
    int money;

public:
    person() : age(0), money(0) {}
    person(int val, int val2) : age(val), money(val2) {}
    person(const person& other) {
        this->age = other.age;
        this->money = other.money;
    }
    // 前置++的重载  返回引用
    person& operator++() { // 保证是对一个数进行++
        this->age++;
        this->money++;
        return *this;
    }
    // 后置++的重载 加一个int参数占位符区分 返回值
    person operator++(int) {
        person temp = *this; // 用一个局部变量返回++之前的结果
        this->age++;
        this->money++;
        return temp;
    }
    void print() { cout << age << " " << money; }
};
int main() {
    person p(5, 12);
    ++p;
    p.print();
    return 0;
}

对前++和后++都是operator++,所以为了区分,对于后++来说,需要加一个参数占位符来进行区分。

在返回值这块,前++返回的是引用,因为要的是它进行加一之后的结果,所以可以直接返回这个对象。

而后++则返回的是一个值,因为要返回的是它加一之前的结果,所以需要一个局部变量返回++之前的结果。

4.+=运算符重载

#include <iostream>
using namespace std;
class person {
    int age;
    int money;

public:
    friend ostream& operator<<(ostream& o, const person& p);
    person() : age(0), money(0) {}
    person(int val, int val2) : age(val), money(val2) {}
    person(const person& other) {
        this->age = other.age;
        this->money = other.money;
    }
    person& operator+=(const person& other) {
        this->age += other.age;
        this->money += other.money;
        return *this;
    }
    void print() { cout << age << " " << money; }
};
ostream& operator<<(ostream& o, const person& p) {
    o << p.age << " " << p.money << endl;
    return o;
}
int main() {
    person a(1, 1), b(2, 2), c(3, 3);
    a += b += c;
    cout << a << b << c;
    return 0;
}

+=运算符的返回值也是对象本身,因为会涉及到连续+=的情况,和左移运算符的重载类似。

这样才能实现连续+=的实现。如上面代码运行的结果为:

要注意运算的顺序是从右往左进行计算的。

5.关系运算符和赋值运算符重载

#include <iostream>
using namespace std;
class a {
    int num;
    int* p;

public:
    a() : num(0), p(nullptr) {}
    a(int x) : num(x), p(new int(num)) {}
    a(const a& other) { this->num = other.num; }
    bool operator>(const a& other) {
        if (this->num > other.num)
            return 1;
        else
            return 0;
    }
    bool operator==(const a& other) {
        if (this->num == other.num)
            return 1;
        else
            return 0;
    }
    a& operator=(const a& other) {
        if (p)
            delete p; // 释放原来的堆区内存,拷贝构造没有这一步
        num = other.num;
        p = new int(num); // 指向新的堆区内存
        return *this;
    }
};
int& fun() {
    int a = 2;
    return a;
}
int main() {
    int x = fun();  // 行
    int& y = fun(); // 不行
    a a(2), b(3), c;
    cout << (a == b);
    a = b =c; // 如果类内没有实现赋值运算符,编译器则会提供默认的赋值运算符,每个成员变量都会被赋值,此时要注意浅拷贝问题
    return 0;
}

关系运算符主要包括==、>、<等,他们的返回值都是bool类型。

赋值运算符,就是把other的值给调用它的对象,如果不对赋值运算符进行重载的话,那么它就会是简单的赋值操作,这里就会导致浅拷贝的问题(可以看作者前面提到的浅拷贝和深拷贝问题)。所以我们就需要对它进行重载。

在上述代码中,因为p是一块指向堆区的指针变量,所以在进行赋值运算符重载时,需要开辟一块新的堆区,然后保证两个堆区的内容一样。同时,还有保证之前这个指针变量p有没有指向别的堆区内容,如果有,就将它释放,避免内存泄漏。返回值为引用类型,返回对象本身。

注意:赋值运算符也是编译器给一个类至少添加的函数。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

  • QT使用QChart绘制面积图

    QT使用QChart绘制面积图

    绘制面积图,则系列选择面积系列qareaseries需要给系列设置上折线和下折线(qlineseries),如果没有设置下折线,则默认x轴为下折线1、创建图表视... [阅读全文]
  • QT使用QChart绘制饼图

    QT使用QChart绘制饼图

    饼图没有坐标轴,也不需要坐标轴使用的系列是qpieseries饼图里面一个一个的块称为切片举例:绘制一个饼图,点击对应切片可以让切片分离1、创建图表视图并开启抗... [阅读全文]
  • 使用Qt实现旋转动画效果

    使用Qt实现旋转动画效果

    使用qpropertyanimation类绑定对应的属性后就可以给这个属性设置对应的动画//比如自定义了属性q_property(int rotation re... [阅读全文]
  • 关于Qt C++中connect的几种写法代码示例

    关于Qt C++中connect的几种写法代码示例

    前言这connect函数就像是编程世界里的“茴”字,千变万化,各有千秋。咱们程序员呢,就像是孔乙己那样,虽然有时候会觉得这些变化有些好笑... [阅读全文]
  • C语言字符函数与字符串函数的实现示例

    1. 字符函数在c语言标准库中提供了一系列用于处理字符的函数,这些函数定义在 <ctype.h>头文件中。字符函数分为两种:字符分类函数和字符转换函数1.1 字符分类函…

    2024年11月05日 编程语言
  • C语言内存函数的具体使用

    前言上期我们学习了,但是字符串函数仅仅只能对字符进行操作,那么这次我们来学习一下内存函数在c语言中内存函数是一组用于内存操作的标准库函数,它们定义在 <string.h>…

    2024年11月05日 编程语言

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com