zoukankan      html  css  js  c++  java
  • 条款10:令operator=返回一个reference to *this

    关于赋值,我们知道可以连续的进行赋值如:

             int x, y, z; x = y = z = 12;

    为了实现连锁的赋值,赋值操作符必须返回一个reference指向操作符的左侧实参,这是你要为class实现赋值操作符时,应该遵循的协议:

    class Widget {

    public :

             ...

             Widget& operator= (const Widget& rhs){

                       ...

                       return *this;

             }

    }

     

    这个协议不仅适用于以上的标准赋值形式,也适用于所有的赋值相关运算,例如:

    class Widget {

    public :

             ...

             Widget& operator= (const Widget& rhs){

                       ...

                       return *this;

             }

             Widget& operator+= (const Widget& rhs){

                       ...

                       return *this;

             }

    }

     

     

    请记住:

    • 令赋值操作符返回一个reference to *this
  • 相关阅读:
    矩阵乘法(二):利用矩阵快速幂运算完成递推
    更改codeblock编译后程序的图标
    如何在VS2008下使用FLTK
    Python type() 函数
    Python range() 函数用法
    Python len()方法
    Python filter() 函数
    Python bool() 函数
    数据类型
    JAVA标识符
  • 原文地址:https://www.cnblogs.com/loveyakamoz/p/2772380.html
Copyright © 2011-2022 走看看