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

  • 相关阅读:
    【论文阅读】Transformer及其在计算机视觉领域上的应用
    【学习笔记】asyncio的使用
    【论文阅读】Grad-CAM: Visual Explanations from Deep Networks via Gradient-based Localization
    【论文阅读】Bag of Tricks for Image Classification with Convolutional Neural Networks
    【论文阅读】Bag of Tricks and A Strong Baseline for Deep Person Re-identification
    【论文阅读】主动学习 (Active Learning)
    可能有点用的东西
    .vimrc
    莫比乌斯反演 学习笔记
    对拍
  • 原文地址:https://www.cnblogs.com/sky-z/p/9502548.html
Copyright © 2011-2022 走看看