zoukankan      html  css  js  c++  java
  • C++网易云课堂开发工程师-操作符重载

    1.操作符重载,(可以使用成员函数,也可以使用非成员函数) this

    所有的成员函数均隐藏着一个参数this.

    this与调用者相互绑定。

    complex c1,c2;                                                                                         对于两个复数的相加,暗含着左边加到右边。

    inline complex&

    complex::operator += (this, const complex& r){                                 this通常为隐藏的

      return _doapl(this, r);

    }

    2.return by reference 语法分析

    inline complex&

    _doapl(complex* ths, const complex& r){

      return *ths;

    }

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    inline complex&                                                                                                  这一部分可以拥有返回类型,同样也可以不使用返回类型。

    complex::operator += (const complex& r){                                                     但是,如果要使用连续加法,那么将不再能够使用了例如:C3 += C2 += C1;

      return __doapl(this,r);

    }

    3.非成员函数的运算符重载

    inline complex                                                                                                      这个地方为什么不使用引用呢?

    operator + (const complex& x, const complex& y){

      return complex(real(x) + real(y), imag(x) + imag(y));

    }

    ~~~~~~~~为什么不返回引用呢~~~~~~~~~

    因为返回的东西,是location的,是临时对象。

    typename()                       complex()                                                                    创建临时的对象,重要!

  • 相关阅读:
    Python异步任务模块之-celery
    Atom 编辑器侧边栏忽略隐藏文件
    判断字符串是否为回文 python
    python 命令行工具 fire
    Appium自动化测试-iOS
    视频转换工具ffmpeg
    nodejs顺序执行shell
    Jenkins 邮箱配置及问题解决
    mac配置php
    appium镜像设置
  • 原文地址:https://www.cnblogs.com/sky-z/p/9502548.html
Copyright © 2011-2022 走看看