• <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.
            88久久精品无码一区二区毛片 | 国产精品99久久久久久猫咪 | 精品久久久无码中文字幕| 国产精久久一区二区三区| 伊人久久大香线蕉AV一区二区| 一本色综合网久久| 国产L精品国产亚洲区久久 | 亚洲AV无码一区东京热久久| 久久精品国产亚洲欧美| 91麻豆国产精品91久久久| 久久电影网一区| 亚洲中文字幕无码一久久区| 精品久久久久中文字| 国产91色综合久久免费| 久久精品国产亚洲AV忘忧草18| 狠狠精品久久久无码中文字幕| 久久一日本道色综合久久| 亚洲一级Av无码毛片久久精品| 97精品伊人久久久大香线蕉 | 久久久久亚洲AV无码专区桃色| 久久精品无码一区二区无码| 亚洲性久久久影院| 久久久久久国产精品无码下载| 国产精品久久久久久搜索| 性色欲网站人妻丰满中文久久不卡| 国产精自产拍久久久久久蜜| 久久91精品国产91久久户| 欧洲精品久久久av无码电影| 久久免费视频1| 国产精品久久久香蕉| 亚洲成av人片不卡无码久久| 久久99精品久久久久久水蜜桃| 国产精品久久成人影院| 久久99国内精品自在现线| 久久人人爽人人爽人人片av高请| 99久久99久久精品国产片果冻| 国产精品久久久香蕉| 精品多毛少妇人妻AV免费久久| 久久亚洲AV无码精品色午夜| 18岁日韩内射颜射午夜久久成人| 亚洲欧洲日产国码无码久久99|