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


            亚洲午夜精品久久久久久人妖| 亚洲精品无码久久千人斩| 成人国内精品久久久久一区| 亚洲国产精品无码久久一区二区| 久久精品人人做人人爽电影| 97精品伊人久久久大香线蕉| 国产精品久久亚洲不卡动漫| 久久久国产精品| 中文精品久久久久人妻不卡| 97久久久精品综合88久久| 国产精品丝袜久久久久久不卡| 久久综合色老色| 亚洲午夜久久久精品影院| 奇米影视7777久久精品人人爽| 好属妞这里只有精品久久| 香蕉久久久久久狠狠色| 国产午夜久久影院| 欧美黑人激情性久久| 午夜精品久久久久久久无码| 国产99精品久久| 久久亚洲日韩精品一区二区三区| 久久精品国产精品亚洲| 99国产精品久久| 久久精品中文闷骚内射| 青青草原综合久久大伊人| 久久精品亚洲欧美日韩久久| 好久久免费视频高清| 久久久久无码精品国产不卡| 人妻无码精品久久亚瑟影视| 久久久综合九色合综国产| 精品久久久久久久无码| 性欧美丰满熟妇XXXX性久久久| 亚洲欧美另类日本久久国产真实乱对白| 久久香蕉一级毛片| 久久91精品久久91综合| 国产精品99久久久久久人| 欧洲成人午夜精品无码区久久| A级毛片无码久久精品免费| 精品国产乱码久久久久软件| 中文字幕久久精品| 久久久久亚洲av成人网人人软件 |