zoukankan      html  css  js  c++  java
  • Effective C++ 条款10

    令operator=返回一个reference to *this

    将operator=返回一个reference是为了什么呢?答案非常easy,就是为了实现连锁形式。

    什么是连锁形式。如int x。y,z。x=y=z=15;这样的形式就是连锁形式。

    声明一下。这仅仅是个大家一致允许的写法。你也能够不遵守这样的写法。

    但是不管是内置类型还是标准库的类型,都遵循这条规则。

    为了达到程序的一致性。也是遵守的比較好。

    下面是涉及的代码:

    #include<iostream>
    using namespace std;
    
    class Widget
    {
    
    public:
        Widget()
        {
            cout<<"Default Ctor"<<endl;
        }
        Widget(const Widget& rhs)
        {
            cout<<"Copy Ctor"<<endl;
        }
        Widget& operator=(const Widget& rhs)
        {
            cout<<"operator="<<endl;
            return *this;
        }
    };
    int main()
    {
        Widget a,b,c;
        a=b=c;
        return 0;
    }
  • 相关阅读:
    单例模式
    js事件
    oracle_to_excel
    jquery_2
    jquery_1
    4.linux 复制,删除,重命名
    一个tomcat下部署多个springboot项目
    3.centos7 安装oracle
    桥接模式
    组合模式
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6985245.html
Copyright © 2011-2022 走看看