七、模板与泛型编程
条款43:学习处理模板化基类内的名称
开始,先写一个程序,它能够传送信息到若干不同的公司去。信息要么就是被译成密码,要么就是未经过加工的文字。
如果在编译期内,我们拥有足够的信息来决定哪一个信息传递到哪一家公司,就可以采用template的方法。
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);
...
};
... // 针对其他公司设计的类
class MsgInfo { ... }; // 保存信息,以备将来产生信息
templ