0
点赞
收藏
分享

微信扫一扫

effectiveC++ 条款43案例

阎小妍 2022-01-31 阅读 52
c++
#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;
}
举报

相关推荐

0 条评论