【C++深度解析】20、操作符重载


如果我们直接使用 “+” 完成复数的加法,要怎么做呢?

这就要使用操作符的重载功能了,操作符的重载是以函数的方式进行的。本质上是用特殊形式的函数扩展操作符的功能。

重载通过 operator 关键字定义特殊的函数,可以重载为类的成员函数和非成员函数。

1 重载为成员函数

语法:
在这里插入图片描述
将操作符重载函数定义为类的成员函数时

  • 比定义为非成员函数少一个参数
  • 编译器优先在成员函数中寻找操作符重载函数
// 20-1.cpp
#include<stdio.h>
class Complex
{
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    int getA() { return a; }
    int getB() { return b; }
    Complex operator + (const Complex& p)
    {
        Complex ret;
        ret.a = this->a + p.a;
        ret.b = this->b + p.b;
        return ret;
    }
private:
    int a, b;
};
int main()
{
    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = c1 + c2;		// 等价于 Complex c3 = c1.operator +(c2);
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    return 0;
}

2 重载为非成员函数

语法:
在这里插入图片描述
将操作符重载函数定义为类的非成员函数需要借助友元的帮助。

//  20-2.cpp
#include<stdio.h>
class Complex
{
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    int getA() { return a; }
    int getB() { return b; }
    friend Complex operator + (Complex& p1, Complex& p2);
private:
    int a, b;
};
Complex operator + (Complex& p1, Complex& p2)
{
    Complex ret;
    ret.a = p1.a + p2.a;
    ret.b = p1.b + p2.b;
    return ret;
}
int main()
{
    Complex c1(1, 2);
    Complex c2(3, 4);
    Complex c3 = c1 + c2;		// 等价于 Complex c3 = operator +(c1, c2);
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    return 0;
}

3 完整的复数类

有了上面运算符重载的知识,我们来设计一个完整的复数类,复数类应该具有的操作有

  • 运算:+,-,*,/
  • 比较:==,!=
  • 赋值:=
  • 求模:modulus

利用运算符的重载,设计成员函数:
在这里插入图片描述

// Complex.h
#ifndef _COMPLEX_H_
#define _COMPLEX_H_
class Complex
{
public:
    Complex(double a = 0, double b = 0);
    double getA();
    double getB();
    double getModulus();

    Complex operator + (const Complex& c);
    Complex operator - (const Complex& c);
    Complex operator * (const Complex& c);
    Complex operator / (const Complex& c);
    
    bool operator == (const Complex& c);
    bool operator != (const Complex& c);
    
    Complex& operator = (const Complex& c);
private:
    double a;
    double b;
};
#endif
// Complex.cpp
#include"Complex.h"
#include<math.h>
Complex::Complex(double a, double b)		// 默认的参数实现时不需要写了
{
    this->a = a;
    this->b = b;
}
double Complex::getA()
{
    return a;
}
double Complex::getB()
{
    return b;
}
double Complex::getModulus()
{
    return sqrt(a*a + b*b);
}
Complex Complex::operator + (const Complex& c)
{
    double na = a + c.a;
    double nb = b + c.b;
    Complex ret(na, nb);
    return ret;
}
Complex Complex::operator - (const Complex& c)
{
    double na = a - c.a;
    double nb = b - c.b;
    Complex ret(na, nb);
    return ret;
}
Complex Complex::operator * (const Complex& c)
{
    double na = a * c.a - b * c.b;
    double nb = a * c.b + b * c.a;
    Complex ret(na, nb);
    return ret;
}
Complex Complex:: operator / (const Complex& c)
{
    double m = c.a * c.a + c.b * c.b;
    double na = (a * c.a + b * c.b) / m;
    double nb = (b * c.a - a * c.b) / m;
    Complex ret(na, nb);
    return ret;

}
bool Complex::operator == (const Complex& c)
{
    return a == c.a && b == c.b;
}
bool Complex::operator != (const Complex& c)
{
    return !(*this == c);
}
Complex& Complex::operator = (const Complex& c)
{
    if (this != &c)
    {
        a = c.a;
        b = c.b;
    }
    return *this;
}
// 20-3.cpp
#include<stdio.h>
#include"Complex.h"
int main()
{
    Complex c1(1, 2);
    Complex c2(3, 6);
    Complex c3 = c2 - c1;
    Complex c4 = c1 * c2;
    Complex c5 = c2 / c1;
    printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
    printf("c4.a = %f, c4.b = %f\n", c4.getA(), c4.getB());
    printf("c5.a = %f, c5.b = %f\n", c5.getA(), c5.getB());
    Complex c6(2, 4);
    printf("c3 == c6 : %d\n", c3 == c6);
    printf("c3 != c4 : %d\n", c3 != c4);
    c2 = c1;
    printf("c2.a = %f, c2.b = %f\n", c2.getA(), c2.getB()); 
    return 0;
}

编译运行:

$ g++ 20-3.cpp Complex.cpp -o 20-3
$ ./20-3
c3.a = 2.000000, c3.b = 4.000000
c4.a = -9.000000, c4.b = 12.000000
c5.a = 3.000000, c5.b = 0.000000
c3 == c6 : 1
c3 != c4 : 1
c2.a = 1.000000, c2.b = 2.000000

注意:

  • C++ 规定赋值操作符(=)只能重载为成员函数
  • 操作符重载不能改变原操作符的优先级,不能改变操作数的个数,也不应该改变操作符的原有语义

4 小结

1、通过 operator 关键字实现重载
2、重载的本质是通过函数扩展操作符的功能
3、全局函数和成员函数都可以实现对操作符的重载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值