条款43:学习处理模板化基类内的名称
代码:

#pragma once #include <iostream> #include <string> using namespace std; class CompanyA { public: CompanyA() { } virtual ~CompanyA() { } void sendCleartext(const std::string& msg){ cout << "CompanyA.sendCleartext:" << msg << endl; } void sendEncrypted(const std::string& msg){ cout << "CompanyA.sendEncrypted:" << msg << endl; } }; //------------------------------------- #pragma once #include <iostream> #include <string> using namespace std; class CompanyB { public: CompanyB() { } virtual ~CompanyB() { } void sendCleartext(const std::string& msg){ cout << "CompanyB.sendCleartext:" << msg << endl; } void sendEncrypted(const std::string& msg){ cout << "CompanyB.sendEncrypted:" << msg << endl; } }; //------------------------------------- #pragma once #include <iostream> #include <string> using namespace std; class CompanyC { public: CompanyC() { } virtual ~CompanyC() { } //void sendCleartext(const std::string& msg){ // cout << "CompanyC.sendCleartext:" << msg << endl; //} void sendEncrypted(const std::string& msg){ cout << "CompanyC.sendEncrypted:" << msg << endl; } }; //------------------------------------- #pragma once #include <string> using std::string; class MsgInfo { string msg; public: MsgInfo(string msg) : msg(msg) { } ~MsgInfo() { } string getMsg(bool bEncrypt = false) const { if (bEncrypt){ return "[encrypt]" + msg; } return msg; } }; //------------------------------------- #pragma once #include "MsgInfo.h" template<class Company> class MsgSender { public: MsgSender() { } virtual ~MsgSender() { } void sendClear(const MsgInfo& msgInfo) { std::string msg; msg = msgInfo.getMsg(); Company c; c.sendCleartext(msg); } void sendSecret(const MsgInfo& msgInfo) { std::string msg; msg = msgInfo.getMsg(true); Company c; c.sendEncrypted(msg); } }; //特化 template<> class MsgSender<CompanyC> { public: MsgSender() { } virtual ~MsgSender() { } void sendSecret(const MsgInfo& msgInfo) { std::string msg; msg = msgInfo.getMsg(true); CompanyC c; c.sendEncrypted(msg); } }; //------------------------------------- #pragma once #include <iostream> #include "MsgSender.h" using std::cout; template<class Company> class LoggingMsgSender : protected MsgSender<Company> { public: LoggingMsgSender() { } virtual ~LoggingMsgSender() { } void sendClearMsg(const MsgInfo& msgInfo) { cout << "sendClear之前 写 log 。。。\n"; //sendClear(msgInfo);//注:使用的VS2013,可以编译通过。(书上说会编译报错。可能之后的优化了) this->sendClear(msgInfo); cout << "sendClear之后 写 log 。。。\n"; } void sendSecretMsg(const MsgInfo& msgInfo) { cout << "sendSecret之前 写 log 。。。\n"; sendSecret(msgInfo);//注:使用的VS2013,可以编译通过。(书上说会编译报错。可能之后的优化了)。 cout << "sendSecret之后 写 log 。。。\n"; } }; //------------------------------------- #include <iostream> #include "CompanyA.h" #include "CompanyB.h" #include "CompanyC.h" #include "MsgSender.h" #include "LoggingMsgSender.h" using namespace std; int main(){ //debug 探测内存泄露 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); //int* p = new int; MsgInfo msgInfo("hello world."); // MsgSender<CompanyA> ma; // ma.sendClear(msgInfo); // ma.sendSecret(msgInfo); // // MsgSender<CompanyB> mb; // mb.sendClear(msgInfo); // mb.sendSecret(msgInfo); // // MsgSender<CompanyC> mc; //// mc.sendClear(msgInfo); // mc.sendSecret(msgInfo); LoggingMsgSender<CompanyA> lma; lma.sendClearMsg(msgInfo); lma.sendSecretMsg(msgInfo); return 0; } //-------------------------------------
++
常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。
昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。
分类:
[1] C&Cpp
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2020-12-22 批处理(.bat文件)的使用
2018-12-22 MFC使用Access数据库