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

            常用鏈接

            統計

            最新評論

            Effective C++ 學習歷程002

                  既然開始了,那就繼續吧,每一個單元包含其中的5個條款。
            主要內容就是代碼的了,但是代碼中也包含了一些注釋。而這些
            代碼都是為各自對應的條款服務的,有些可以值得推敲。希望對
            你有些幫助。ok,謝謝!
              1 // ====================================================================
              2 // --- 條款06:若不想實驗編譯器自動生成的函數,就該明確拒絕
              3 // 注意:
              4 // 1    為駁回編譯器自動(暗自)提供的機能,可將相應的成員函數聲明為private or protected
              5 //      并且不予實現。使用像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:為多態基類聲明virtual 析構函數 
             23 // 注意:
             24 // 1    polymorphic(多態的)base class 應該聲明一個virtual 析構函數。
             25 //      如果class帶有任何virtual函數,她應該擁有一個virtual析構函數
             26 // 2    class 的設計目的如果不是作為base class使用,或不是為了具備多態性
             27 //      就不應該聲明一個virtual 析構函數 
             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:別讓異常逃離析構函數 
             51 // 注意:
             52 // 1    析構函數絕對不要吐出異常。如果一個唄析構函數調用的函數可能拋出異常,
             53 //      那么在析構函數內應該捕捉任何異常,然后吞下它們(不傳播)或是結束程序
             54 // 2    如果客戶需要對某個操作函數運行期拋出的異常做出反應,那么class 應該提供
             55 //      一個普通的函數(而非在系后函數中)執行該操作 
             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構造和析構過程中調用virtual 函數 
            104 // 注意:
            105 // 1    在構造和析構過程中不要調用virtual函數,因為這類調用從不下降只derived class
            106 //      (比起當前執行構造和析構函數的那一次 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= 返回一個 referance to *this
            181 // 注意:
            182 // 1    令賦值(assignment)操作符返回一個 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 閱讀(203) 評論(0)  編輯 收藏 引用 所屬分類: 學習研究瑣碎編程

            日韩乱码人妻无码中文字幕久久| 蜜桃麻豆www久久| 欧美午夜A∨大片久久| 一本久久精品一区二区| 婷婷伊人久久大香线蕉AV| 国产精品久久网| 欧美国产成人久久精品| 综合久久国产九一剧情麻豆| 久久精品中文字幕无码绿巨人 | 久久99国产精一区二区三区| 狠狠久久综合伊人不卡| 欧美牲交A欧牲交aⅴ久久| 欧美激情精品久久久久久| 久久国产免费观看精品3| 久久精品国产清自在天天线| 日产精品久久久久久久性色| 久久久久亚洲av成人无码电影 | 国产精品久久久久AV福利动漫| 午夜不卡888久久| 久久亚洲精品人成综合网| 久久久精品人妻无码专区不卡| 精品久久久久久国产潘金莲| 久久亚洲天堂| 丰满少妇人妻久久久久久4| 人妻丰满?V无码久久不卡| 精品精品国产自在久久高清| 少妇内射兰兰久久| 2021国产精品久久精品| 性高湖久久久久久久久AAAAA| 99久久国产热无码精品免费久久久久| 久久综合综合久久综合| 中文国产成人精品久久不卡| 区亚洲欧美一级久久精品亚洲精品成人网久久久久| 久久福利青草精品资源站| 久久人人爽人人爽人人片AV不| 久久国产欧美日韩精品| 久久99九九国产免费看小说| 2021国内精品久久久久久影院| 久久毛片一区二区| 久久精品国产99国产精品导航 | 亚洲伊人久久大香线蕉苏妲己|