• <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>
            posts - 9, comments - 0, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
            base class constructors execute before derived class constructors, derived class data members have not been initialized when base class constructors run. If virtual functions called during base class construction went down to derived classes, the derived class functions would almost certainly refer to local data members, but those data members would not yet have been initialized.Calling down to parts of an object that have not yet been initialized is inherently dangerous, so C++ gives you no way to do it.

            #include <iostream>
            #include <string>
            #include <cstdlib>
            void print(std::string str){std::cout << str<< std::endl;}
            class Transaction {
                public:
                    Transaction()
                    {
                        print("Transaction Constructor");
                        logTransaction();
                    }
                    virtual void logTransaction() const // =0;
                    {
                        print("Transaction Log");
                    }
            };
            class BuyTransaction: public Transaction
            {
                public:
                    BuyTransaction(){   print("BuyTransaction Constructor");}
                    virtual void logTransaction() const
                    {
                        print("BuyTransaction Log");
                    }
            };
            int main()
            {
                BuyTransaction dbc;
                //dbc.logTransaction();
            }
            pure virtual functions cannot link.
            [shangtang@BTSOM-1 study]$ g++ TestT.cpp
            TestT.cpp: In constructor 'Transaction::Transaction()':
            TestT.cpp:14: warning: abstract virtual 'virtual void Transaction::logTransaction() const' called from constructor
            /tmp/ccXFzaHv.o: In function `Transaction::Transaction()':
            TestT.cpp:(.text._ZN11TransactionC2Ev[Transaction::Transaction()]+0x7f): undefined reference to `Transaction::logTransaction() const'
            collect2: ld returned 1 exit status
            virtual function can compile, run, but with surprise result
            [shangtang@BTSOM-1 study]$ ./a.out
            Transaction Constructor
            Transaction Log
            BuyTransaction Constructor

            The only way to avoid this problem is to make sure that none of your constructors or destructors call virtual functions on the object being created or destroyed and that all the functions they call obey the same constraint.
            国产L精品国产亚洲区久久| 久久久久久久精品成人热色戒| 久久精品无码一区二区无码| 精品免费tv久久久久久久| 精品国产综合区久久久久久| 久久久久久久波多野结衣高潮 | 久久九九全国免费| 久久笫一福利免费导航 | 久久精品国产清自在天天线| 国产精品岛国久久久久| 久久久久亚洲AV无码观看| 欧美亚洲另类久久综合| 香蕉久久av一区二区三区| 国产日韩欧美久久| 国产精品久久永久免费| 漂亮人妻被中出中文字幕久久| 中文字幕亚洲综合久久| 久久精品九九亚洲精品| 久久婷婷是五月综合色狠狠| 久久国产成人午夜aⅴ影院 | 精品无码久久久久久午夜| 欧美日韩精品久久久久| 国产69精品久久久久99尤物| 国产精品久久久久影院色| 色综合久久无码中文字幕| 青青草原综合久久大伊人| 久久午夜福利电影| 国产亚洲成人久久| 午夜不卡888久久| 久久综合久久综合九色| 久久精品亚洲中文字幕无码麻豆| 伊人久久国产免费观看视频 | 国产精品国色综合久久| 久久久久人妻精品一区| 久久精品黄AA片一区二区三区| 国内高清久久久久久| 亚洲国产另类久久久精品小说| 伊人久久大香线蕉亚洲| 久久精品国产亚洲77777| 狠狠色丁香久久综合婷婷| 99久久精品久久久久久清纯|