• <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》學習筆記 Item 2 Prefer iostream to stdio.h

            關鍵詞: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>是全局意義上的庫函數聲明,容易沖突。
            #include<iostream>是STL的寫法,要名字空間std::聲明。


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

            《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 @ 2007-12-10 14:36 rednight 閱讀(219) | 評論 (0)編輯 收藏

            2007年12月6日 #

            《effective c++ II》Introduction

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

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

            僅列出標題  
            久久久久久久久无码精品亚洲日韩 | 久久丫精品国产亚洲av| 嫩草伊人久久精品少妇AV| 成人国内精品久久久久一区| 国产精品99久久99久久久| 久久婷婷国产麻豆91天堂| 欧美午夜A∨大片久久 | 精品久久久久久久国产潘金莲| 久久久久亚洲AV成人网人人网站 | 色婷婷噜噜久久国产精品12p| 久久综合鬼色88久久精品综合自在自线噜噜| 久久久久一本毛久久久| 久久久久免费看成人影片| 免费观看久久精彩视频| 无码人妻久久一区二区三区蜜桃 | 国产精品99久久久久久猫咪| 久久久精品国产免大香伊| 久久婷婷久久一区二区三区| 国产精品久久久久蜜芽| 久久国产成人午夜aⅴ影院 | 久久精品国产欧美日韩99热| 精品久久人妻av中文字幕| 无码八A片人妻少妇久久| 激情五月综合综合久久69| 国产精品久久久久影视不卡| 国产69精品久久久久9999APGF| 久久99亚洲综合精品首页| 一本色道久久88加勒比—综合| 午夜精品久久久久久久| 久久精品卫校国产小美女| 久久强奷乱码老熟女网站| 99久久国产免费福利| 伊人色综合久久| 久久国产乱子伦精品免费午夜| 久久九九亚洲精品| 韩国三级大全久久网站| 精品九九久久国内精品| 精品综合久久久久久97超人 | 亚洲狠狠综合久久| 99久久精品免费观看国产| 99久久夜色精品国产网站|