• <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++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
            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.

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            93精91精品国产综合久久香蕉| 国产精品99久久免费观看| 久久WWW免费人成—看片| 久久久久亚洲精品无码网址| 国产成人精品综合久久久久| 69国产成人综合久久精品| 久久久久亚洲精品男人的天堂| 国产成人久久精品一区二区三区 | 久久精品国产一区二区| 久久中文字幕人妻丝袜| 久久九九青青国产精品| 久久乐国产综合亚洲精品| 久久综合久久综合久久| 久久精品国产久精国产一老狼| 人人狠狠综合久久亚洲婷婷| 亚洲国产精品无码成人片久久| 国产亚洲精午夜久久久久久| 久久成人国产精品| 亚洲国产欧美国产综合久久| 日韩中文久久| 久久天天躁狠狠躁夜夜2020老熟妇 | 99久久免费国产特黄| 久久成人小视频| 婷婷久久综合九色综合九七| 国产精品九九久久免费视频| 99麻豆久久久国产精品免费| 性高湖久久久久久久久| 中文字幕久久久久人妻| 国内高清久久久久久| 日本WV一本一道久久香蕉| 一本色综合久久| 国产香蕉久久精品综合网| 久久亚洲国产精品123区| 久久久久久青草大香综合精品| 国产成人久久精品麻豆一区| 韩国无遮挡三级久久| 一本一道久久精品综合| 国产69精品久久久久9999| 久久人妻少妇嫩草AV无码蜜桃| 手机看片久久高清国产日韩| 久久久久国产|