• <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>

            千里之行,始于足下

            2007年12月10日 #

            《effective c++ II》學(xué)習(xí)筆記 Item 2 Prefer iostream to stdio.h

            關(guān)鍵詞:safety, extensibility

            int i;
            Rational r;                           
            // r is a rational number

            cin 
            >> i >> r;
            cout 
            << i << r;


            class Rational {
            public:
              Rational(
            int numerator = 0int denominator = 1);
              
            private:
              
            int n, d;    // numerator and denominator
            friend ostream& operator<<(ostream& s, const Rational& r);
            };
            ostream
            & operator<<(ostream& s, const Rational& r)
            {
              s 
            << r.n << '/' << r.d;
              
            return s;
            }

            繞開scanf(),printf()惱人的格式要求。使用便捷,安全的<<,>>操作符。

            最后,
            #include<iostream.h>是全局意義上的庫函數(shù)聲明,容易沖突。
            #include<iostream>是STL的寫法,要名字空間std::聲明。


            posted @ 2007-12-10 16:12 rednight 閱讀(243) | 評論 (0)編輯 收藏

            《effective c++ II》學(xué)習(xí)筆記 Item1 Prefer const and inline to #define

            宏定義
            #define ASPECT_RATIO 1.653

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


            值得注意的是老的編譯器不支持這種表達(dá)方式,因此要采用如下的方式:
             
            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; }

            增強(qiáng)適應(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 @ 2007-12-10 14:36 rednight 閱讀(218) | 評論 (0)編輯 收藏

            2007年12月6日 #

            《effective c++ II》Introduction

            內(nèi)容見http://www.shnenglu.com/rednight/articles/37913.html
            為啥只有隨筆能顯示在主頁面上呢?

            posted @ 2007-12-06 17:08 rednight 閱讀(264) | 評論 (0)編輯 收藏

            僅列出標(biāo)題  
            欧美亚洲日本久久精品| 少妇被又大又粗又爽毛片久久黑人 | 国产精品久久久久久| 9999国产精品欧美久久久久久| 国内精品伊人久久久久| 亚洲精品国精品久久99热一| 久久精品亚洲乱码伦伦中文| 久久国产成人午夜aⅴ影院| 99久久精品这里只有精品| 浪潮AV色综合久久天堂| 99久久免费国产精精品| 亚洲精品国产第一综合99久久| 亚洲欧美久久久久9999 | jizzjizz国产精品久久| 久久久久亚洲精品天堂| 国产精品美女久久久久久2018| 精品免费久久久久久久| 精品久久久久久国产免费了| 2021国产精品午夜久久| 久久九九久精品国产免费直播| 狠狠色丁香久久综合婷婷| 伊人久久综合精品无码AV专区| 狠狠色丁香久久婷婷综合图片| 成人综合久久精品色婷婷| 久久亚洲日韩精品一区二区三区| 久久久久久夜精品精品免费啦| 久久99国产精品成人欧美| 一本久道久久综合狠狠躁AV| 久久久久18| 久久99国产精品99久久 | 99久久伊人精品综合观看| 中文字幕一区二区三区久久网站| 青青草原综合久久大伊人导航| 97久久香蕉国产线看观看| 久久夜色撩人精品国产| 一本大道加勒比久久综合| 偷窥少妇久久久久久久久| 无码精品久久久天天影视| 无码人妻少妇久久中文字幕| 久久乐国产综合亚洲精品| 久久亚洲私人国产精品vA|