• <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》學(xué)習(xí)筆記 Item1 Prefer const and inline to #define

            宏定義
            #define ASPECT_RATIO 1.653

            主要是宏在預(yù)編譯的時候被移除,不加入編譯器編譯,不利于錯誤的檢測,給調(diào)試維護帶來一定困難。
            因而用
            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;


            這種情形下如果要在類中定義常量數(shù)組,需要采用枚舉類型做一折中處理:
            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))

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

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

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

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

            精品久久久久久无码中文野结衣| 色99久久久久高潮综合影院 | 2021最新久久久视精品爱| 久久精品国产亚洲欧美| 久久久久久国产a免费观看黄色大片| 精品久久久一二三区| 国产精品久久久久久福利69堂| 91精品国产91久久久久福利| 国产高清国内精品福利99久久| 亚洲午夜精品久久久久久app| 亚洲愉拍99热成人精品热久久| 99精品久久久久久久婷婷| 久久国产劲爆AV内射—百度| 久久精品无码免费不卡| 精品视频久久久久| AAA级久久久精品无码区| 色综合久久久久综合体桃花网 | 麻豆av久久av盛宴av| 久久久久久综合一区中文字幕| 久久综合狠狠综合久久综合88| 久久婷婷色香五月综合激情| 久久久久亚洲精品天堂久久久久久| 久久久久亚洲AV综合波多野结衣| 久久综合综合久久狠狠狠97色88| 国产精品久久网| 伊人久久综在合线亚洲2019| 2021精品国产综合久久| 久久国产一区二区| 综合久久给合久久狠狠狠97色 | 久久91综合国产91久久精品| 国产精品无码久久综合| 久久精品国产亚洲麻豆| 久久久久国色AV免费看图片| 欧美伊人久久大香线蕉综合69| 18禁黄久久久AAA片| 久久综合九色综合久99| 欧美日韩精品久久久免费观看 | 久久精品国产亚洲AV大全| 99久久伊人精品综合观看| 亚洲中文久久精品无码ww16| 久久精品国产精品亚洲人人|