• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            千里之行,始于足下

            《effective c++ II》學習筆記 Item1 Prefer const and inline to #define

            宏定義
            #define ASPECT_RATIO 1.653

            主要是宏在預編譯的時候被移除,不加入編譯器編譯,不利于錯誤的檢測,給調試維護帶來一定困難。
            因而用
            const double ASPECT_RATIO = 1.653;

            代替。
            存在2個小問題需要注意:
            1)    指針常量問題
            const char * const authorName = "Scott Meyers";

            需要2次使用”const”
            2)    在類的定義中
            class GamePlayer {
            private:
              
            static const int NUM_TURNS = 5;    // constant declaration
              int scores[NUM_TURNS];             // use of constant
              
            };

            此外,還必須在.cpp文件中予以定義:
            const int GamePlayer::NUM_TURNS;      // mandatory definition;
                                                  
            // goes in class impl. file


            值得注意的是老的編譯器不支持這種表達方式,因此要采用如下的方式:
             
            class EngineeringConstants {      // this goes in the class
            private:                          // header file
              static const double FUDGE_FACTOR;
              
            };

            // this goes in the class implementation file
            const double EngineeringConstants::FUDGE_FACTOR = 1.35;


            這種情形下如果要在類中定義常量數組,需要采用枚舉類型做一折中處理:
            class GamePlayer {
            private:
              
            enum { NUM_TURNS = 5 };    // "the enum hack" — makes
                                         
            // NUM_TURNS a symbolic name
                                         
            // for 5
              int scores[NUM_TURNS];     // fine

            };


            避免
            #define max(a,b) ((a) > (b) ? (a) : (b))

            這種寫法。
            采用內連函數:
            inline int max(int a, int b) { return a > b ? a : b; }

            增強適應性,應采用模板類:
            template<class T>
            inline 
            const T& max(const T& a, const T& b)
            return a > b ? a : b; }

            總結:并不是說不使用宏,要明確地知道使用宏后可能會引起的后果,減少有歧義的情況發生。
             

            posted on 2007-12-10 14:36 rednight 閱讀(227) 評論(0)  編輯 收藏 引用

            久久久久久久精品妇女99 | 伊人久久大香线蕉无码麻豆| 夜夜亚洲天天久久| 久久国产乱子伦精品免费午夜| 日韩久久无码免费毛片软件| 久久人人爽人人爽人人片av麻烦 | 欧美一区二区三区久久综合| 91久久婷婷国产综合精品青草 | 93精91精品国产综合久久香蕉| 久久精品视频91| 国产精品久久久久9999| 久久影院午夜理论片无码| 久久久久亚洲AV成人片| 久久受www免费人成_看片中文 | 精品无码久久久久久久久久| 亚洲愉拍99热成人精品热久久| 狠狠久久综合伊人不卡| 久久久国产精华液| 一本久久免费视频| 热RE99久久精品国产66热| 91秦先生久久久久久久| a高清免费毛片久久| 99久久精品免费看国产一区二区三区 | 国产精品99久久99久久久| 亚洲αv久久久噜噜噜噜噜| 久久久久久av无码免费看大片| AAA级久久久精品无码片| 99精品久久久久久久婷婷| 麻豆精品久久久久久久99蜜桃 | 国产999精品久久久久久| 国产亚洲精久久久久久无码| 一本久久a久久精品vr综合| 久久久久久亚洲精品无码| 久久久久久国产精品美女| 无码乱码观看精品久久| 亚洲人成电影网站久久| 久久婷婷是五月综合色狠狠| 亚洲日韩中文无码久久| 日本久久久久亚洲中字幕| 久久国产高潮流白浆免费观看| 精品熟女少妇a∨免费久久|