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

            void main() { }

            Hello ,world !

            常用鏈接

            統(tǒng)計(jì)

            最新評(píng)論

            Effective C++ 學(xué)習(xí)歷程002

                  既然開始了,那就繼續(xù)吧,每一個(gè)單元包含其中的5個(gè)條款。
            主要內(nèi)容就是代碼的了,但是代碼中也包含了一些注釋。而這些
            代碼都是為各自對(duì)應(yīng)的條款服務(wù)的,有些可以值得推敲。希望對(duì)
            你有些幫助。ok,謝謝!
              1 // ====================================================================
              2 // --- 條款06:若不想實(shí)驗(yàn)編譯器自動(dòng)生成的函數(shù),就該明確拒絕
              3 // 注意:
              4 // 1    為駁回編譯器自動(dòng)(暗自)提供的機(jī)能,可將相應(yīng)的成員函數(shù)聲明為private or protected
              5 //      并且不予實(shí)現(xiàn)。使用像Uncopyable這樣的base class 也是一種做法
              6 namespace testItem06
              7 {
              8     class Uncopyable
              9     {
             10     protected:
             11         Uncopyable() {}
             12         ~Uncopyable() {}
             13     private:
             14         Uncopyable(const Uncopyable&) ;
             15         Uncopyable& operator = (const Uncopyable&) ;
             16     } ;
             17     class HomeForSale : private Uncopyable
             18     { // 
             19     } ;
             20 }//namespace testItem06
             21 // ====================================================================
             22 // --- 條款07:為多態(tài)基類聲明virtual 析構(gòu)函數(shù) 
             23 // 注意:
             24 // 1    polymorphic(多態(tài)的)base class 應(yīng)該聲明一個(gè)virtual 析構(gòu)函數(shù)。
             25 //      如果class帶有任何virtual函數(shù),她應(yīng)該擁有一個(gè)virtual析構(gòu)函數(shù)
             26 // 2    class 的設(shè)計(jì)目的如果不是作為base class使用,或不是為了具備多態(tài)性
             27 //      就不應(yīng)該聲明一個(gè)virtual 析構(gòu)函數(shù) 
             28 // 3
             29 namespace testItem07
             30 {
             31     class TimeKeeper
             32     {
             33     public:
             34         TimeKeeper() {};
             35         virtual ~TimeKeeper() {};
             36         // should virtual for delete pBase ;//destroy exactly
             37         // 
             38     } ;
             39     class AtomicClock : public TimeKeeper{ } ;// 
             40     class WaterClock : public TimeKeeper{ } ;// 
             41     class WristClock : public TimeKeeper{ } ;// 
             42     class AWOV
             43     {
             44     public:
             45         virtual ~AWOV() = 0 ;
             46     } ;
             47     AWOV::~AWOV() {} //must has a defalut definition
             48 }
             49 // ====================================================================
             50 // --- 條款08:別讓異常逃離析構(gòu)函數(shù) 
             51 // 注意:
             52 // 1    析構(gòu)函數(shù)絕對(duì)不要吐出異常。如果一個(gè)唄析構(gòu)函數(shù)調(diào)用的函數(shù)可能拋出異常,
             53 //      那么在析構(gòu)函數(shù)內(nèi)應(yīng)該捕捉任何異常,然后吞下它們(不傳播)或是結(jié)束程序
             54 // 2    如果客戶需要對(duì)某個(gè)操作函數(shù)運(yùn)行期拋出的異常做出反應(yīng),那么class 應(yīng)該提供
             55 //      一個(gè)普通的函數(shù)(而非在系后函數(shù)中)執(zhí)行該操作 
             56 // 3
             57 namespace testItem08
             58 {
             59     class DBConnection
             60     {
             61         // .
             62     public:
             63         void start() {}
             64         void close() {}
             65         // .
             66     } ;
             67     class DBConn
             68     {
             69     private:
             70         bool   bClose_ ;
             71         DBConnection db ;
             72     public:
             73         void start() //
             74         {
             75             db.start() ;
             76             bClose_ = false ;
             77         }
             78         void close() // better .note 2
             79         {
             80             db.close() ;
             81             bClose_ = true ;
             82         }
             83         ~DBConn()// better .
             84         {
             85             if(!bClose_)
             86             {
             87                 try
             88                 {
             89                     db.close() ;
             90                 }
             91                 catch()
             92                 {// better .note 1
             93                     handleError() ;// or: db.close() again 
             94                     bClose_ = false ;
             95                 }
             96             }
             97         }
             98     protected:
             99         void handleError() {} // 
            100     } ;
            101 }//namespace testItem08
            102 // ====================================================================
            103 // --- 條款09:絕不vuz構(gòu)造和析構(gòu)過程中調(diào)用virtual 函數(shù) 
            104 // 注意:
            105 // 1    在構(gòu)造和析構(gòu)過程中不要調(diào)用virtual函數(shù),因?yàn)檫@類調(diào)用從不下降只derived class
            106 //      (比起當(dāng)前執(zhí)行構(gòu)造和析構(gòu)函數(shù)的那一次 class)
            107 // 2
            108 // 3
            109 namespace testItem09
            110 {
            111     class Transaction_Bad
            112     {
            113     private:
            114         int     value_ ;
            115     public:
            116         Transaction_Bad() ;
            117         virtual void logTransaction_Bad() const = 0 ;
            118     } ;
            119     Transaction_Bad::Transaction_Bad()
            120     {   // 
            121         //logTransaction_Bad() ; // warn !!! or error
            122     }
            123     void Transaction_Bad::logTransaction_Bad() const
            124     { std::cout << "  virtual void Transaction_Bad::logTransaction_Bad() const\n" ; }
            125     class BuyTransaction_Bad : public Transaction_Bad
            126     {
            127     public:
            128         //BuyTransaction_Bad():Transaction_Bad() {}
            129         virtual void logTransaction_Bad() const
            130         { std::cout << "  virtual void BuyTransaction_Bad::logTransaction_Bad() const\n" ; }
            131     } ;
            132     class SellTransaction_Bad : public Transaction_Bad
            133     {
            134     public:
            135         //BuyTransaction_Bad():Transaction_Bad() {}
            136         virtual void logTransaction_Bad() const
            137         { std::cout << " virtual void SellTransaction_Bad::logTransaction_Bad() const\n" ; }
            138     } ;
            139     // --------------
            140     class Transaction_Good
            141     {
            142     public:
            143         explicit Transaction_Good(const std::string& logInfo)  ;
            144         void logTransaction_Good(const std::string& logInfo) const  ;//non-virtual
            145     } ;
            146     Transaction_Good::Transaction_Good(const std::string& logInfo)
            147     {
            148         // 
            149         logTransaction_Good(logInfo) ; // warn !!!
            150     }
            151     void Transaction_Good::logTransaction_Good(const std::string& logInfo) const
            152     {
            153         std::cout << " void Transaction_Good::logTransaction_Good(const std::string& logInfo) const\n"
            154                   << " logInfo: " << logInfo << "\n" ;
            155     }
            156     class BuyTransaction_Good : public Transaction_Good
            157     {
            158     private:
            159         static string createLogStr(int index)
            160         { return " static string BuyTransaction_Good::createLogStr(int index)" ; }
            161     public:
            162         BuyTransaction_Good(int index):Transaction_Good(createLogStr(index))
            163         {} // 
            164         virtual void logTransaction_Good() const
            165         { std::cout << " virtual void BuyTransaction_Good::logTransaction_Good() const\n" ; }
            166     } ;
            167     class SellTransaction_Good : public Transaction_Good
            168     {
            169     private:
            170         static string createLogStr(int index)
            171         { return " static string SellTransaction_Good::createLogStr(int index)" ; }
            172     public:
            173         SellTransaction_Good(int index):Transaction_Good(createLogStr(index))
            174         {} // 
            175         virtual void logTransaction_Good() const
            176         { std::cout << " virtual void SellTransaction_Good::logTransaction_Good() const\n" ; }
            177     } ;
            178 }//namespace testItem09
            179 // ====================================================================
            180 // --- 條款10:令operator= 返回一個(gè) referance to *this
            181 // 注意:
            182 // 1    令賦值(assignment)操作符返回一個(gè) reference tio *this 
            183 namespace testItem10
            184 {
            185     class Widget
            186     {
            187     private:
            188         int value_ ;
            189     public:
            190         //Widget():value_(0) {} //  or remove
            191         Widget(int v=0):value_(v) {}
            192         Widget& operator = (const Widget& rhs) ;
            193         Widget& operator += (const Widget& rhs) ;
            194         void printW()
            195         { std::cout << " Widget::value_: " << value_ << "\n" ; }
            196         // 
            197     } ;
            198     Widget& Widget::operator = (const Widget& rhs)
            199     {
            200         value_ = rhs.value_ ;
            201         return *this ;
            202     }
            203     Widget& Widget::operator += (const Widget& rhs)
            204     {
            205         value_ += rhs.value_ ;
            206         return *this ;
            207     }
            208 }


            posted on 2010-05-30 15:35 only 閱讀(208) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 學(xué)習(xí)研究瑣碎編程

            久久久久国产精品嫩草影院| 久久99国产精品久久| 久久久不卡国产精品一区二区| 精品久久香蕉国产线看观看亚洲| 天天躁日日躁狠狠久久 | 人妻无码精品久久亚瑟影视| 精品久久久久久久久中文字幕| 狠狠色婷婷久久一区二区三区| 色欲综合久久躁天天躁蜜桃| 久久久精品人妻一区二区三区蜜桃| 国产成人精品免费久久久久| 久久久亚洲欧洲日产国码aⅴ| 日本久久久久久中文字幕| 久久久精品无码专区不卡| 思思久久99热免费精品6| 久久精品人人做人人爽电影| 久久人人爽人人爽人人AV| 94久久国产乱子伦精品免费| 久久99这里只有精品国产| 久久Av无码精品人妻系列| 一级做a爰片久久毛片看看| 国产一区二区三区久久| 久久综合亚洲鲁鲁五月天| 青青青国产精品国产精品久久久久| 亚洲第一永久AV网站久久精品男人的天堂AV | 国产成人精品久久一区二区三区av | 日本高清无卡码一区二区久久| 无码人妻少妇久久中文字幕 | 亚洲国产精品综合久久网络| 久久久久久久久波多野高潮| 久久久亚洲AV波多野结衣| 久久天天躁狠狠躁夜夜不卡| 国产毛片久久久久久国产毛片| 思思久久精品在热线热| 久久久女人与动物群交毛片| 久久97久久97精品免视看秋霞| 国产午夜精品理论片久久影视| 免费无码国产欧美久久18| 久久久99精品一区二区| 久久人人爽人爽人人爽av| 久久91亚洲人成电影网站|