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

    赋值的连锁式 x=y=z=3;被解析成x=(y=(z=3));为了实现“连锁赋值”,赋值操作符必须返回一个reference指向操作符的左侧实参,

    这是你为class实现赋值操作符时应该遵循的协议:

    class Widget
    {
    public:
        Widget& operator=(const Widget& rhs) //返回类型是reference,
        {                                                            //指向当前对象
            ...
            return *this;                //返回左侧对象
        }
    protected:
    private:
    };

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

    class Widget
    {
    public:
        Widget& operator+=(const Widget& rhs) //返回类型是reference,
        {                                                            //指向当前对象
            ...
            return *this;                //返回左侧对象
        }
        Widget& operator=(int rhs)
        {
            ...
            return *this;
        }
    protected:
    private:
    };

    令赋值操作符返回一个reference to *this。

  • 相关阅读:
    objective-C nil,Nil,NULL 和NSNull的小结
    Calendar控件点击下个月按钮后,本月标记的各个具体天的样式都取消
    如何让Button的Text垂直居中显示
    html基础总结2
    html基础总结1
    html基础总结
    微信空白页获取用户openid
    网址
    网站式更新后台代码
    JavaScriptSerializer引用
  • 原文地址:https://www.cnblogs.com/lidan/p/2321819.html
Copyright © 2011-2022 走看看