• <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.
            亚洲中文字幕无码久久2017| 久久久免费观成人影院 | 99久久国产热无码精品免费| 狼狼综合久久久久综合网| 久久免费线看线看| 色偷偷88欧美精品久久久| 久久久久亚洲av无码专区喷水| 国产精品福利一区二区久久| 大香网伊人久久综合网2020| 午夜精品久久久久| 97久久精品午夜一区二区| 亚洲精品无码专区久久同性男| 久久精品人人做人人妻人人玩 | 99久久婷婷国产综合亚洲| 久久久久18| 久久久久亚洲精品无码蜜桃| 久久国产香蕉视频| 99久久婷婷国产综合亚洲| 久久久国产视频| 久久久久一级精品亚洲国产成人综合AV区| 久久精品国产男包| 久久精品国产亚洲av瑜伽| 国产成人无码久久久精品一| 狠狠色噜噜色狠狠狠综合久久 | 国内精品久久国产大陆| 伊人久久精品无码av一区 | 久久久亚洲欧洲日产国码aⅴ| 久久狠狠一本精品综合网| 久久青青草原国产精品免费| 亚洲狠狠婷婷综合久久久久| 欧美久久一区二区三区| 久久国产乱子伦精品免费午夜| 久久久精品一区二区三区| …久久精品99久久香蕉国产| 久久精品aⅴ无码中文字字幕重口| 久久91精品国产91久| 欧美日韩久久中文字幕| 久久狠狠爱亚洲综合影院| 久久人人爽人人爽人人片AV麻烦| 久久国产AVJUST麻豆| 免费精品久久天干天干|