你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

C++——操作符重载

[复制链接]
gaosmile 发布时间:2020-8-13 21:23
一、操作符重载:
1、我们先来看一个问题实现,下面的复数解决方案是否可行,复数大家应该都不陌生(分为实部和虚部):
class Complex
{
   public:
         int a;
         int b;
};

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

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

代码版本一:
#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;
}

运行结果:
root@txp-virtual-machine:/home/txp# ./a.out
c3.a=4,c3,b=6

这里通过Add函数可以解决Complex对象相加的问题,但是在我们数学运算里面就是直接实部加实部,虚部加虚部,和正常的实数相加一样,所以说,为什么不直接这样操作呢,这就涉及到符号"+"的问题。
2、操作重载符的引出
  • c++中的重载能够扩展操作符的功能
  • 操作符的重载以函数的方式进行
  • 本质:用特殊形式的函数扩展操作符的功能

3、操作重载符的语法:
  • 通过operator关键字可以定义特殊的函数
  • operator的本质是通过函数重载操作符
  • 语法格式:

Type operator Sign(const Type& p1,const Type& p2)
{
      Type ret;
      
      return ret;
}

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

代码版本二:
#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;
}

输出结果:
root@txp-virtual-machine:/home/txp# ./a.out
c3.a=4,c3,b=6

4、再次改进代码:
  • 可以将操作符重载函数定义成为类的成员函数(前面我们学过,友元现代软件开发不允许)
  • 比全局操作符重载函数少一个参数(左操作数,成员函数中隐藏的 this 参数可以充当左操作数的角色)
  • 不需要依赖友元就可以完成操作符重载
  • 编译器优先在成员函数中寻找操作符重载(一旦在成员函数中找到,就不会去全局找)

class Type
{
   public:
      Type operator Sign(const Type& p)
      {
          Type ret;
          return ret;
      }
};

代码版本三:
#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;
}

输出结果:
root@txp-virtual-machine:/home/txp# ./a.out
Complex operator +(const Complex& p)
c3.a=4,c3,b=6

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

好了,今天的分享就到这里,如果文章中有错误或者不理解的地方,可以交流互动,一起进步。
收藏 评论0 发布时间:2020-8-13 21:23

举报

0个回答

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版