絕不在構(gòu)造和析構(gòu)過程中調(diào)用virtual函數(shù)

字號:

首先明確一下,對于一個(gè)繼承體系,構(gòu)造函數(shù)是從基類開始調(diào)用了,而析構(gòu)函數(shù)則正好相反,從最外層的類開始。
    對于在構(gòu)造函數(shù)中調(diào)用virtual函數(shù),先舉個(gè)例子:
    1 class Transaction //所有交易的基類
    2 {
    3 public:
    4   Transaction();
    5   virtual void logTransaction() const = 0;//日志記錄,因交易類型的不同而有不同的記錄
    6 }
    7
    8 Transaction::Transaction()//構(gòu)造函數(shù)實(shí)現(xiàn)
    9 {
    10
    11   logTransaction();//調(diào)用了日志記錄
    12 }
    13
    14 class Sell: public Transaction
    15 {
    16 public:
    17   virtual void logTransaction() const;
    18
    19 }
    Sell類從基類中繼承,這時(shí)候如果執(zhí)行:
    1 Sell a; //派生類
    則首先會(huì)執(zhí)行Transaction的構(gòu)造函數(shù),而Transaction構(gòu)造函數(shù)會(huì)調(diào)用Transaction版本加入收藏的logTransaction函數(shù)(記?。夯悩?gòu)造函數(shù)中的virtual函數(shù)不會(huì)下降到派生類中)。
    而大家都知道,基類中的logTransaction還沒有實(shí)現(xiàn)代碼,這顯然會(huì)產(chǎn)生一個(gè)連接錯(cuò)誤。
    有如下的解決方法:將logTransaction聲明為非virtual函數(shù),然后通過派生類向基類傳遞參數(shù)的方法來實(shí)現(xiàn)。
    1 class Transaction
    2 {
    3 public:
    4   Transaction(const std::string& logInfo);
    5   void logTransaction(const std::string& logInfo) const;//改成非virtual實(shí)現(xiàn)
    6
    7 };
    8
    9 Transaction::Transaction(const std::string& logInfo)
    10 {
    11
    12   logTransaction(logInfo);//同樣在構(gòu)造函數(shù)中調(diào)用
    13 }
    14
    15 class Sell: public Transaction
    16 {
    17 public:
    18   Sell()
    19     :Transaction(createLog())//將log信息傳給基類構(gòu)造函數(shù)
    20   {
    21
    22   }
    23 }
    如此一來,就是派生類將構(gòu)造信息向上傳給基類構(gòu)造函數(shù),解決了這個(gè)問題。