前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++之操作符重载学习总结

C++之操作符重载学习总结

作者头像
用户6280468
发布2022-03-21 14:44:03
2660
发布2022-03-21 14:44:03
举报
文章被收录于专栏:txp玩Linux

一、操作符重载:

1、我们先来看一个问题实现,下面的复数解决方案是否可行,复数大家应该都不陌生(分为实部和虚部):

代码语言:javascript
复制
class Complex
{
   public:
         int a;
         int b;
};

/*--------------------------*/
int main()
{
   Complex c1 = {1,2};
   Complex c2 = {3,4};
   Complex c3 = c1 + c2;//也就是复数的实部加实部,虚部叫虚部。
   
   return 0;
}

注:成员变量为公有且没有自定义构造函数的时候,可以通过大括号来分别初始     化成员变量;

代码版本一:

代码语言:javascript
复制
#include <stdio.h>

class Complex 
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    
    int getA()
    {
        return a;
    }
    
    int getB()
    {
        return b;
    }
    
    friend Complex Add(const Complex& p1, const Complex& p2);
};

Complex Add(const Complex& p1, const 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 = Add(c1, c2); // c1 + c2
    
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    
    return 0;
}

运行结果:

代码语言:javascript
复制
root@txp-virtual-machine:/home/txp# ./a.out
c3.a=4,c3,b=6

这里通过Add函数可以解决Complex对象相加的问题,但是在我们数学运算里面就是直接实部加实部,虚部加虚部,和正常的实数相加一样,所以说,为什么不直接这样操作呢,这就涉及到符号"+"的问题。

2、操作重载符的引出

  • c++中的重载能够扩展操作符的功能
  • 操作符的重载以函数的方式进行
  • 本质:用特殊形式的函数扩展操作符的功能

3、操作重载符的语法:

  • 通过operator关键字可以定义特殊的函数
  • operator的本质是通过函数重载操作符
  • 语法格式:
代码语言:javascript
复制
Type operator Sign(const Type& p1,const Type& p2)
{
      Type ret;
      
      return ret;
}

注:Sign为系统中预定义的操作符,比如:+、-、*、/ 等

代码版本二:

代码语言:javascript
复制
#include <stdio.h>

class Complex 
{
    int a;
    int b;
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 + (const Complex& p1, const Complex& p2);
};

Complex operator + (const Complex& p1, const 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; // operator + (c1, c2)
    
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    
    return 0;
}

输出结果:

代码语言:javascript
复制
root@txp-virtual-machine:/home/txp# ./a.out
c3.a=4,c3,b=6

4、再次改进代码:

  • 可以将操作符重载函数定义成为类的成员函数(前面我们学过,友元现代软件开发不允许)
  • 比全局操作符重载函数少一个参数(左操作数,成员函数中隐藏的 this 参数可以充当左操作数的角色)
  • 不需要依赖友元就可以完成操作符重载
  • 编译器优先在成员函数中寻找操作符重载(一旦在成员函数中找到,就不会去全局找)
代码语言:javascript
复制
class Type
{
   public:
      Type operator Sign(const Type& p)
      {
          Type ret;
          return ret;
      }
};

代码版本三:

代码语言:javascript
复制
#include <stdio.h>

class Complex 
{
    int a;
    int b;
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;
        printf("Complex operator + (const Complex& p)\n");
        ret.a = this->a + p.a;
        ret.b = this->b + p.b;
        
        return ret;
    }
    
    friend Complex operator + (const Complex& p1, const Complex& p2);
};

Complex operator + (const Complex& p1, const Complex& p2)
{
    Complex ret;
    printf("Complex operator + (const Complex& p1, const Complex& p2)\n");
    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; // c1.operator + (c2)
    
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    
    return 0;
}

输出结果:

代码语言:javascript
复制
root@txp-virtual-machine:/home/txp# ./a.out
Complex operator +(const Complex& p)
c3.a=4,c3,b=6

二、总结:

  • 操作符重载是c++的强大特性之一
  • 操作符重载的本质是通过函数扩展操作符的功能
  • operator 关键字是实现操作符重载的关键
  • 操作符重载遵循相同的函数重载规则
  • 全局函数和成员函数都可以实现对操作符的重载

好了,今天的分享就到这里,如果文章中有错误或者不理解的地方,可以交流互动,一起进步。我是txp,下期见!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-08-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 txp玩Linux 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档