• <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.
            亚洲精品乱码久久久久久按摩| 久久国产精品久久久| 91精品国产高清久久久久久91| 久久毛片免费看一区二区三区| 国产精品久久国产精品99盘| 久久婷婷国产综合精品| 久久久久国产一级毛片高清版| 久久精品国产一区二区三区不卡 | 久久精品国产只有精品2020| 国产成人无码久久久精品一| 狠狠色丁香婷婷综合久久来来去 | 东京热TOKYO综合久久精品| 精品久久久久久亚洲| 一本一本久久a久久精品综合麻豆| 亚洲AV日韩精品久久久久久久| 久久综合给久久狠狠97色| 精品无码人妻久久久久久| 人妻无码精品久久亚瑟影视| 天天综合久久久网| 久久久久亚洲精品无码蜜桃| 久久青青草视频| 精品久久人人爽天天玩人人妻 | 久久九九亚洲精品| 久久婷婷五月综合97色直播| 精品久久久久久中文字幕| 亚洲精品美女久久久久99| 亚洲国产精品无码久久久久久曰| 热久久国产精品| 久久精品国产亚洲AV无码麻豆 | 欧美日韩精品久久久久| 99久久免费国产精品热| 久久丫精品国产亚洲av| 无码国内精品久久综合88| 亚洲精品成人久久久| 色播久久人人爽人人爽人人片aV | 麻豆精品久久久久久久99蜜桃| 成人国内精品久久久久一区| 2021久久精品免费观看| 色8激情欧美成人久久综合电| 伊人久久五月天| 久久精品免费全国观看国产|