人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統(tǒng)下載站 > 技術開發(fā)教程 > 詳細頁面

C++箴言:訪問模板化基類中名字

C++箴言:訪問模板化基類中名字

更新時間:2022-08-03 文章作者:未知 信息來源:網(wǎng)絡 閱讀次數(shù):

假設我們要寫一個應用程序,它可以把消息傳送到幾個不同的公司去。消息既可以以加密方式也可以以明文(不加密)的方式傳送。如果我們有足夠的信息在編譯期間確定哪個消息將要發(fā)送給哪個公司,我們就可以用一個 template-based(模板基)來解決問題:

  class CompanyA {
  public:
  ...
  void sendCleartext(const std::string& msg);
  void sendEncrypted(const std::string& msg);
  ...
  };

  class CompanyB {
  public:
  ...
  void sendCleartext(const std::string& msg);
  void sendEncrypted(const std::string& msg);
  ...
  };
  ... // classes for other companies

  class MsgInfo { ... }; // class for holding information
  // used to create a message
  template
  class MsgSender {
  public:
  ... // ctors, dtor, etc.

  void sendClear(const MsgInfo& info)
  {
  std::string msg;
  create msg from info;

  Company c;
  c.sendCleartext(msg);
  }
  void sendSecret(const MsgInfo& info) // similar to sendClear, except
  { ... } // calls c.sendEncrypted
  };

  這個能夠很好地工作,但是假設我們有時需要在每次發(fā)送消息的時候把一些信息記錄到日志中。通過一個 derived class(派生類)可以很簡單地增加這個功能,下面這個似乎是一個合理的方法:

  template
  class LoggingMsgSender: public MsgSender {
  public:
  ... // ctors, dtor, etc.
  void sendClearMsg(const MsgInfo& info)
  {
  write "before sending" info to the log;
  sendClear(info); // call base class function;
  // this code will not compile!
  write "after sending" info to the log;
  }
  ...
  };

  注意 derived class(派生類)中的 message-sending function(消息發(fā)送函數(shù))的名字 (sendClearMsg) 與它的 base class(基類)中的那個(在那里,它被稱為 sendClear)不同。這是一個好的設計,因為它避開了 hiding inherited names(隱藏繼承來的名字)的問題(參見《C++箴言:避免覆蓋通過繼承得到的名字》)和重定義一個 inherited non-virtual function(繼承來的非虛擬函數(shù))的與生俱來的問題(參見《C++箴言:絕不重定義繼承的非虛擬函數(shù)》)。但是上面的代碼不能通過編譯,至少在符合標準的編譯器上不能。這樣的編譯器會抱怨 sendClear 不存在。我們可以看見 sendClear 就在 base class(基類)中,但編譯器不會到那里去尋找它。我們有必要理解這是為什么。

  問題在于當編譯器遇到 class template(類模板)LoggingMsgSender 的 definition(定義)時,它們不知道它從哪個 class(類)繼承。當然,它是 MsgSender,但是 Company 是一個 template parameter(模板參數(shù)),這個直到更遲一些才能被確定(當 LoggingMsgSender 被實例化的時候)。不知道 Company 是什么,就沒有辦法知道 class(類)MsgSender 是什么樣子的。特別是,沒有辦法知道它是否有一個 sendClear function(函數(shù))。

  為了使問題具體化,假設我們有一個要求加密通訊的 class(類)CompanyZ:


  class CompanyZ { // this class offers no
  public: // sendCleartext function
  ...
  void sendEncrypted(const std::string& msg);
  ...
  };

  一般的 MsgSender template(模板)不適用于 CompanyZ,因為那個模板提供一個 sendClear function(函數(shù))對于 CompanyZ objects(對象)沒有意義。為了糾正這個問題,我們可以創(chuàng)建一個 MsgSender 針對 CompanyZ 的特化版本:

  template<> // a total specialization of
  class MsgSender { // MsgSender; the same as the
  public: // general template, except
  ... // sendCleartext is omitted
  void sendSecret(const MsgInfo& info)
  { ... }
  };

  注意這個 class definition(類定義)開始處的 "template <>" 語法。它表示這既不是一個 template(模板),也不是一個 standalone class(獨立類)。正確的說法是,它是一個用于 template argument(模板參數(shù))為 CompanyZ 時的 MsgSender template(模板)的 specialized version(特化版本)。這以 total template specialization(完全模板特化)聞名:template(模板)MsgSender 針對類型 CompanyZ 被特化,而且這個 specialization(特化)是 total(完全)的——只要 type parameter(類型參數(shù))被定義成了 CompanyZ,就沒有剩下能被改變的其它 template's parameters(模板參數(shù))。

[1] [2]  下一頁

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 南京市| 合作市| 商河县| 营口市| 武宣县| 观塘区| 宜川县| 沙湾县| 卢湾区| 阿拉尔市| 赞皇县| 宁都县| 沙洋县| 英吉沙县| 宾阳县| 佛教| 苏尼特右旗| 榆林市| 乐陵市| 巴楚县| 连州市| 宜兰县| 沙洋县| 丹巴县| 酉阳| 敦化市| 美姑县| 迁西县| 柳河县| 舟山市| 高唐县| 乌兰察布市| 汉川市| 肇源县| 新蔡县| 宝山区| 三原县| 台前县| 抚远县| 盐津县| 同江市|