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


            人人狠狠综合久久亚洲高清| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 久久久久亚洲AV无码麻豆| 久久精品人人做人人爽97| 国产成人久久精品二区三区| 久久乐国产综合亚洲精品| 狠色狠色狠狠色综合久久| 久久精品无码一区二区三区免费| 久久无码专区国产精品发布| 久久久久久免费一区二区三区| 亚洲精品tv久久久久| 亚洲国产成人久久综合碰碰动漫3d| 欧美精品一区二区久久 | 久久久久成人精品无码 | 国产成人精品久久一区二区三区av | 国产AⅤ精品一区二区三区久久| 欧美黑人激情性久久| 久久99精品久久久久久不卡| 久久香蕉超碰97国产精品| 午夜精品久久影院蜜桃| 久久精品国产99久久久香蕉| 国产一区二区三区久久精品| 久久夜色精品国产噜噜麻豆| 一本久久免费视频| 亚洲国产成人精品女人久久久 | 亚洲国产精品久久| 久久精品这里热有精品| 精品午夜久久福利大片| 国产精品一久久香蕉国产线看观看 | 亚洲欧美一区二区三区久久| 久久精品成人欧美大片| 久久久久国产视频电影| 久久国产热这里只有精品| 国産精品久久久久久久| 国产2021久久精品| 久久国产精品国语对白| 亚洲狠狠久久综合一区77777| 高清免费久久午夜精品| 婷婷久久综合九色综合98| 午夜不卡888久久| 四虎国产精品免费久久|