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
    }
  • 相关阅读:
    移动方法
    linux主编号的动态分配
    linux 分配和释放设备编号
    linux设备编号的内部表示
    linux主次编号
    linux模块参数
    linux scull 的设计
    linux模块加载竞争
    linux清理函数
    linux初始化中的错误处理
  • 原文地址:https://www.cnblogs.com/zoneofmine/p/14921496.html
Copyright © 2011-2022 走看看