• <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.
            久久综合综合久久97色| 一本色道久久99一综合| 国产午夜精品理论片久久| 免费精品久久久久久中文字幕| 伊人 久久 精品| 69国产成人综合久久精品| 国产成人精品久久一区二区三区av | 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 中文精品99久久国产| 国产亚洲精久久久久久无码| 久久久WWW成人| 69久久夜色精品国产69| 久久久久亚洲AV片无码下载蜜桃| 久久久久人妻一区精品色 | 伊人久久大香线焦综合四虎| 思思久久99热免费精品6| 久久综合久久综合九色| 伊人久久综合精品无码AV专区| 国产巨作麻豆欧美亚洲综合久久| 亚洲国产另类久久久精品小说 | 久久精品国产亚洲av影院| 久久婷婷五月综合色99啪ak| 久久99久久99小草精品免视看| 久久99久久99精品免视看动漫| 狠狠精品久久久无码中文字幕| 国产精品久久亚洲不卡动漫| 精品熟女少妇AV免费久久| 亚洲精品无码久久久久AV麻豆| 久久久久综合国产欧美一区二区| 久久99精品国产自在现线小黄鸭| 亚洲国产精品久久久天堂| 国产A级毛片久久久精品毛片| 日韩中文久久| 亚洲国产精品无码久久久久久曰| 久久久久婷婷| 日日狠狠久久偷偷色综合96蜜桃| 久久91这里精品国产2020| 久久久久黑人强伦姧人妻| 四虎影视久久久免费| 久久久这里有精品| 精品国产99久久久久久麻豆|