• <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.
            日日噜噜夜夜狠狠久久丁香五月| 国内精品久久久久| 国内精品伊人久久久久777| 女人高潮久久久叫人喷水| 亚洲狠狠婷婷综合久久久久| 青青草国产精品久久| 性做久久久久久久久久久| 国产精品久久久久久久久鸭| 日本高清无卡码一区二区久久| 国产精品99精品久久免费| 人妻系列无码专区久久五月天| 精品久久久久香蕉网| 久久亚洲AV成人无码| 久久精品国产WWW456C0M| 国内精品久久久久影院优| 欧美伊人久久大香线蕉综合| 久久九九久精品国产| 久久国产一区二区| 精品综合久久久久久888蜜芽| 亚洲国产婷婷香蕉久久久久久| 免费观看久久精彩视频| 狠狠色丁香婷综合久久| 久久精品国产亚洲AV高清热| 久久人人添人人爽添人人片牛牛| 精品国产青草久久久久福利| 久久se精品一区精品二区| www久久久天天com| 国产精品久久国产精麻豆99网站| 色综合久久久久综合体桃花网| 久久精品国产免费观看| 久久亚洲国产成人影院| 国内精品伊人久久久久妇| 亚洲人成网站999久久久综合| 无码国内精品久久人妻麻豆按摩 | 日韩精品久久久久久久电影蜜臀| 国内精品久久国产| 少妇高潮惨叫久久久久久| 久久久久亚洲精品天堂| 91亚洲国产成人久久精品网址| 精品国产一区二区三区久久蜜臀| 久久久久亚洲?V成人无码|