[翻譯] Effective C++, 3rd Edition, Item 43: 瞭解如何訪問 templatized base classes(模板化基類)中的名字(下)

(點擊此處,接上篇)

已知 MsgSender 針對 CompanyZ 被特化,再次考慮 derived class(派生類)LoggingMsgSender

template<typename Company>
class LoggingMsgSender: public MsgSender<Company> {
public:
  ...
  void sendClearMsg(const MsgInfo& info)
  {
    write "before sending" info to the log;

    sendClear(info);                          // if Company == CompanyZ,
                                              // this function doesn't exist!
    write "after sending" info to the log;
  }
  ...

};

就像註釋中寫的,當 base class(基類)是 MsgSender<CompanyZ> 時,這裏的代碼是無意義的,因爲那個類沒有提供 sendClear function(函數)。這就是爲什麼 C++ 拒絕這個調用:它認識到 base class templates(基類模板)可能被特化,而這樣的 specializations(特化)不一定提供和 general template(通用模板)相同的 interface(接口)。結果,它通常會拒絕在 templatized base classes(模板化基類)中尋找 inherited names(繼承來的名字)。在某種意義上,當我們從 Object-Oriented C++ 跨越到 Template C++(參見 Item 1)時,inheritance(繼承)會停止工作。

爲了重新啓動它,我們必須以某種方式使 C++ 的 "don't look in templatized base classes"(不在模板基類中尋找)行爲失效。有三種方法可以做到這一點。首先,你可以在被調用的 base class functions(基類函數)前面加上 "this->":

template<typename Company>
class LoggingMsgSender: public MsgSender<Company> {
public:

  ...

  void sendClearMsg(const MsgInfo& info)
  {
    write "before sending" info to the log;

    this->sendClear(info);                // okay, assumes that
                                          // sendClear will be inherited
    write "after sending" info to the log;
  }

  ...

};

第二,你可以使用一個 using declaration,如果你已經讀過 Item 33,這應該是你很熟悉的一種解決方案。那個 Item 解釋了 using declarations 如何將被隱藏的 base class names(基類名字)引入到一個 derived class(派生類)範圍中。因此我們可以這樣寫 sendClearMsg

template<typename Company>
class LoggingMsgSender: public MsgSender<Company> {
public:
  using MsgSender<Company>::sendClear;   // tell compilers to assume
  ...                                    // that sendClear is in the
                                         // base class
  void sendClearMsg(const MsgInfo& info)
  {
    ...
    sendClear(info);                     // okay, assumes that
    ...                                  // sendClear will be inherited
  }

  ...
};

(雖然 using declaration 在這裏和 Item 33 中都可以工作,但要解決的問題是不同的。這裏的情形不是 base class names(基類名字)被 derived class names(派生類名字)隱藏,而是如果我們不告訴編譯器去做,它們就不會搜索 base class 範圍。)

最後一個讓你的代碼通過編譯的辦法是顯式指定被調用的函數是在 base class(基類)中的:

template<typename Company>
class LoggingMsgSender: public MsgSender<Company> {
public:
  ...
  void sendClearMsg(const MsgInfo& info)
  {
    ...
    MsgSender<Company>::sendClear(info);      // okay, assumes that
    ...                                       // sendClear will be
  }                                           // inherited

  ...
};

通常這是一個解決這個問題的最不合人心的方法,因爲如果被調用函數是 virtual(虛擬)的,顯式限定會關閉 virtual binding(虛擬綁定)行爲。

從名字可見性的觀點來看,這裏每一個方法都做了同樣的事情:它向編譯器保證任何後繼的 base class template(基類模板)的 specializations(特化)都將支持 general template(通用模板)提供的 interface(接口)。所有的編譯器在解析一個像 LoggingMsgSender 這樣的 derived class template(派生類模板)時,這樣一種保證都是必要的,但是如果保證被證實不成立,真相將在後繼的編譯過程中暴露。例如,如果後面的源代碼中包含這些,

LoggingMsgSender<CompanyZ> zMsgSender;

MsgInfo msgData;

...                                          // put info in msgData

zMsgSender.sendClearMsg(msgData);            // error! won't compile

sendClearMsg 的調用將不能編譯,因爲在此刻,編譯器知道 base class(基類)是 template specialization(模板特化)MsgSender<CompanyZ>,它們也知道那個 class(類)沒有提供 sendClearMsg 試圖調用的 sendClear function(函數)。

從根本上說,問題就是編譯器是早些(當 derived class template definitions(派生類模板定義)被解析的時候)診斷對 base class members(基類成員)的非法引用,還是晚些時候(當那些 templates(模板)被特定的 template arguments(模板參數)實例化的時候)再進行。C++ 的方針是寧願早診斷,而這就是爲什麼當那些 classes(類)被從 templates(模板)實例化的時候,它假裝不知道 base classes(基類)的內容。

Things to Remember

  • 在 derived class templates(派生類模板)中,可以經由 "this->" 前綴,經由 using declarations,或經由一個 explicit base class qualification(顯式基類限定)引用 base class templates(基類模板)中的名字。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章