• <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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(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.
            色综合久久综合网观看| 欧美日韩久久中文字幕| 大美女久久久久久j久久| 久久久久久av无码免费看大片| 伊人久久精品影院| 久久99热精品| 久久精品国产男包| 日本精品久久久久中文字幕8| 一极黄色视频久久网站| 久久久噜噜噜久久中文福利| 精品久久久久久国产牛牛app| 久久只这里是精品66| 激情伊人五月天久久综合| 久久久综合香蕉尹人综合网| 久久无码人妻一区二区三区午夜| 国产成人久久精品麻豆一区| 午夜精品久久久久久久| 久久国产精品免费一区二区三区| 亚洲精品午夜国产VA久久成人| 色综合久久中文色婷婷| 天天爽天天狠久久久综合麻豆| 国产精品免费久久久久久久久| 色婷婷综合久久久中文字幕| 亚洲国产成人精品91久久久| 久久美女网站免费| 国内精品伊人久久久久AV影院| 久久久精品国产| 欧美久久一区二区三区| 99久久精品免费国产大片| 漂亮人妻被黑人久久精品| 欧美日韩精品久久久久| 亚洲国产成人久久综合区| 久久久久婷婷| 一级做a爰片久久毛片毛片| 久久综合给合综合久久| 久久久久亚洲AV成人网人人软件| A级毛片无码久久精品免费| 国产999精品久久久久久| 精品国产婷婷久久久| 久久精品国产只有精品66| 久久精品这里只有精99品|