zoukankan      html  css  js  c++  java
  • Effective C++ 笔记 —— Item 4: Make sure that objects are initialized before they’re used

    In a constructor, prefer use of the member initialization list to assignment inside the body of the constructor. List data members in the initialization list in the same order they're declared in the class.

    The relative order of initialization of non-local static objects defined in different translation units is undefined.

    Avoid initialization order problems across translation units by replacing non-local static objects with local static objects.

    class FileSystem { /*...*/ }; // as before
    FileSystem& tfs() // this replaces the tfs object; it could be
    { // static in the FileSystem class
        static FileSystem fs; // define and initialize a local static object
        return fs; // return a reference to it
    }
    class Directory { /*...*/ }; // as before
    Directory::Directory(params) // as before, except references to tfs are
    { // now to tfs()
        //...
            std::size_t disks = tfs().numDisks();
        //...
    }
    Directory& tempDir() // this replaces the tempDir object; it could be static in the Directory class
    { 
        static Directory td(params); // define/initialize local static object
        return td; // return reference to it
    }
  • 相关阅读:
    squid代理缓存服务
    部署网络存储ISCSI
    电子邮局系统
    使用DHCP动态管理主机地址
    vsftp -samba-autofs
    python初学 | 循环for while
    python初学 | 条件语句if
    python初学 | set
    python初学 | 字典dictionary
    python初学 | 元组tuple
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/14921496.html
Copyright © 2011-2022 走看看