运算符+-*/等重载以及如何选择重载类型(成员函数 或则还是 全局函数)

运算符重载不改变其运算规则和顺序

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

class Complex
{
private:
    double m_a, m_b;
public:
    //以全局函数的形式进行重载
    friend Complex operator+(const Complex &a, const Complex &b);
    friend Complex operator-(const Complex &a, const Complex &b);
    friend Complex operator*(const Complex &a, const Complex &b);
    friend Complex operator/(const Complex &a, const Complex &b);
    friend bool operator==(const Complex &a, const Complex &b);
    friend bool operator!=(const Complex &a, const Complex &b);
    //以成员函数的形式重载+= -= *= /= %=
    Complex operator += (const Complex & a);
    Complex & operator -= (const Complex & a);
    Complex operator *= (const Complex & a);
    Complex operator /= (const Complex & a);
    void show()
    {
        cout << this->m_a << " " << this->m_b << endl;
    }
public:
    Complex(double a, double b) : m_a(a),m_b(b){}
    Complex(){}
};

Complex operator+(const Complex &a, const Complex &b)
{
    return Complex(a.m_a+b.m_a, a.m_b + b.m_b);
}
Complex operator-(const Complex &a, const Complex &b)
{
    return Complex(a.m_a-b.m_a, a.m_b -b.m_b);
}
Complex operator*(const Complex &a, const Complex &b)
{
    return Complex(a.m_a*b.m_a, a.m_b *b.m_b);
}
Complex operator/(const Complex &a, const Complex &b)
{
    return Complex(a.m_a/b.m_a, a.m_b / b.m_b);
}
bool operator==(const Complex &a, const Complex &b)
{
    if(a.m_a == b.m_a && a.m_b ==b.m_b) 
        return true;
    else
        return false;
}
bool operator!=(const Complex &a, const Complex &b)
{
    if(a.m_a != b.m_a || a.m_b !=b.m_b) 
        return true;
    else
        return false;
}

Complex Complex::operator+=(const Complex & a)
{
    this->m_a = this->m_a + a.m_a;
    this->m_b = this->m_b + a.m_b;
    return *this;
}






int main()
{
    Complex a(3,3), b(3.5,2.2), c;
    a += b;
    a += b;
    a.show();
    return 0;
}

代码有点长还是比较好以理解

  • 以全局函数重载的是 + - * / ,原因是 a + b 计算的时候是operator+(a,b);
  • 以成员函数重载的是+= ,原因 a+b 计算显示的是a.operator(b);只用保证b是类就行 如果是 a + 2.2 或 2.2 + a.如果是第二种就是2.2.operator+(b),显然错误,所以避免这样的问题,第二种方式采用全局函数重载
  • 是否返回值是应用的类型,如果是的话,方便下一次的过程能够修改其中的数值,或者是就是同一个内容,可以参考<< , >> 的使用
  • 选择是成员函数 还是 友元函数,除了规则中要求的外,其他的按照这个规则进行读取

一、分析为什么部分操作符是只能够重载为成员函数的
如果运算符当做是全局函数函数重载的话那么只要碰到这个运算符,就会检测左边和右边的对象是否满足运算的过程,如果是就会执行操作,但是如果没有定义赋值操作符重载函数,编译器也会隐式的定义,那么如果在定义,就会产生歧义,所以不行

  • ()
  • =
  • ->
  • 类的this指针会被绑定到运算符的左侧运算对象,成员运算符函数的显示参数比运算符对象总数少一个,这样可以减少程序员的错误。
  • 运算符的左操作数必须是该类类型的参数,换句话说,假设c++编译器允许[]操作符重载函数是全局的,那么程序员完全可能写出容易出错的代码。
  • 一个数字一个构造函数,如上文中的举例

参考:

  1. http://c.biancheng.net/cpp/biancheng/view/3298.html
  2. https://blog.csdn.net/qq_44861675/article/details/105126559
  3. https://blog.csdn.net/qq_38124695/article/details/78188643
  4. https://blog.csdn.net/u011857683/article/details/81879351
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值