• <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   管理


            久久久www免费人成精品| 国产AV影片久久久久久| 久久精品无码一区二区WWW | 国产精品对白刺激久久久| 国产精品无码久久综合| 亚洲狠狠综合久久| 久久99精品久久久大学生| 久久精品人人槡人妻人人玩AV| 亚洲一区二区三区日本久久九| 精品久久久久成人码免费动漫| 久久久老熟女一区二区三区| 久久99精品国产99久久6| 狼狼综合久久久久综合网| 精品熟女少妇aⅴ免费久久| 久久婷婷成人综合色综合| 狠狠色丁香婷婷综合久久来来去 | 精品久久久久久成人AV| 久久播电影网| 99久久99这里只有免费的精品| 久久精品视屏| 99久久精品国产一区二区| 69国产成人综合久久精品| 久久综合久久综合亚洲| 久久99精品久久久久久野外| 97久久精品无码一区二区天美| 国产精品一区二区久久精品涩爱 | 国产精品免费久久| 97精品伊人久久大香线蕉app | 精品久久国产一区二区三区香蕉 | 大香伊人久久精品一区二区| 93精91精品国产综合久久香蕉 | 久久久久久国产精品无码下载 | 九九久久99综合一区二区| 热re99久久6国产精品免费| 无码人妻久久一区二区三区蜜桃| 久久精品无码一区二区三区日韩 | 少妇无套内谢久久久久| 97精品依人久久久大香线蕉97| 久久狠狠爱亚洲综合影院| 亚洲国产精品久久久天堂 | 综合久久一区二区三区 |