学习处理模板化基类内的名称
直接以一段代码来解释
class companyA
{
public:
void sendMsgA();
void sendMsgB();
...
};
class companyB
{
public:
void sendMsgA();
void sendMsgB();
}
class companyC
{
public:
void sendMsgB();
}
class MsgInfo {...};
template <typename Company>
class MsgSender
{
public:
void sendMsg(const MsgInfo& info)
{
std::string msg;
Company c;
c.sendMsgA(msg);//这句话没有问题,将根据sendMsgA具现话。
}
}
template <typename Company>
class LoggingMsgSender:public MsgSender<Company>
{
public:
void sendMyMsg(const MsgInfo& info)
{
//sendMsgA(msg);//编译不过,因为并不知道Company并没有具现话,不一定提供SendMsgA
this->sendMsgA(msg);//加上this指针,假设函数被继承
}
}
模板基类,编译器不会主动到基类去查找名称,需要显示调用this指针。