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()                                                                    创建临时的对象,重要!

  • 相关阅读:
    系统tabbar出现两个tabbar的问题解决方案。
    iOS 开发苹果由http改为https 之后,如果服务器不做相应的修改,那么客户端需要做点更改
    UIAlertController的一些简单实用方法
    ios开发同一个lab显示不同的颜色
    ios开发同一个版本多次提交不想改变版本号的解决方法
    iOS开发textfield的一些方法汇总
    C#笔记
    Shader之性能优化
    Shader之ShaderUI使用方法
    Shader Example
  • 原文地址:https://www.cnblogs.com/sky-z/p/9502548.html
Copyright © 2011-2022 走看看