#include <string>
#include <iostream>
class CompanyA{
public:
void sendClearText(const std::string &msg){std::cout<<"A"<<std::endl;}
void sendSecretText(const std::string &msg);
};
class CompanyB{
public:
void sendClearText(const std::string &msg){std::cout<<"B"<<std::endl;}
void sendSecretText(const std::string &msg);
};
class CompanyZ{
public:
//void sendClearText(const std::string &msg);
void sendSecretText(const std::string &msg){std::cout<<"C Secret"<<std::endl;}
};
class MsgInfo{};
template<class Company>
class MsgSender{
public:
void sendClear(const MsgInfo& info){
std::string msg;
Company c;
c.sendClearText(msg);
}
void sendSecret(const MsgInfo& info){}
};
template<>
class MsgSender<CompanyZ>{
public:
void sendSecret(const MsgInfo& info){
std::string msg;
CompanyZ c;
c.sendSecretText(msg);
}
};
template<class Company>
class LoggingMsgSender:public MsgSender<Company>{
public:
//using MsgSender<Company>::sendClear;
void sendClearMsg(const MsgInfo& info){
//法一:使用this->
//this->sendClear(info);
//法二:using声明
//sendClear(info);
//法三指出被调用的函数位于base class内
MsgSender<Company>::sendClear(info);
}
};
int main(){
//MsgSender<CompanyA> ms;
//ms.sendClear(*new MsgInfo);
MsgSender<CompanyZ> ms;
ms.sendSecret(*new MsgInfo);
std::cout<<"123"<<std::endl;
return 0;
}
effectiveC++ 条款43案例
最新推荐文章于 2024-09-11 19:54:50 发布