• <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.
            久久久久久国产精品无码下载| 国产精品久久久久…| 99精品国产免费久久久久久下载| 欧美激情一区二区久久久| 伊人久久无码中文字幕| 狠狠久久综合| 欧洲成人午夜精品无码区久久| 国产精品gz久久久| 人妻精品久久久久中文字幕一冢本| 亚洲午夜精品久久久久久人妖| 2021国产精品午夜久久| 欧美精品一区二区久久| 99久久国产热无码精品免费久久久久 | 精品国产VA久久久久久久冰| 理论片午午伦夜理片久久 | 中文字幕无码久久人妻| 国产精品99久久久久久宅男| 久久精品国产亚洲AV高清热| 久久久亚洲裙底偷窥综合| 久久久久亚洲精品天堂久久久久久 | 狠狠色伊人久久精品综合网| 国内精品伊人久久久久av一坑| 精品久久久一二三区| 性欧美大战久久久久久久| 久久久久亚洲AV成人网人人软件| 亚洲天堂久久精品| 99热精品久久只有精品| 欧美激情精品久久久久| 久久精品9988| 久久99久久无码毛片一区二区| 欧美精品一区二区精品久久| 久久精品www| 国产精品日韩欧美久久综合| 国产激情久久久久影院老熟女| 伊人久久精品线影院| 国产午夜精品久久久久九九电影| 国产高清美女一级a毛片久久w| 久久91这里精品国产2020| 香蕉aa三级久久毛片| 老男人久久青草av高清| 国产美女久久精品香蕉69|