问题代码
#include <iostream>
using namespace std;
class CompanyA
{
public:
void sendCleartext(const std::string& msg);
void sendEncrypted(const string& msg);
};
class CompanyB
{
public:
void sendCleartext(const std::string& msg);
void sendEncrypted(const string& msg);
};
class MsgInfo{};
template<typename Company>
class MsgSender
{
public:
void sendClear(const MsgInfo& info)
{
string msg;
Company c;
c.sendCleartext(msg);
}
void sendSecret(const MsgInfo& info)
{
string msg;
Company c;
c.sendEncrypted(msg);
}
};
template<typename Company>
class LoggingMsgSender:public MsgSender<Company>
{
public:
void sendClearMsg(const MsgInfo& info)
{
sendClear(info);
}
};
上述代码无法通过编译,至少是对严守规律的编译器而言。这样的编译器会抱怨sendClear不存在。虽然我们看到sendClear确实在基类内,然而编译器就是看不到它们。其原因是因为有一种所谓的模板全特化,在这个特化版的基类里可能没有sendClear这个函数,因此C++拒绝这个调用的原因是:它知道基类模板有可能被特化,而那个特化版本可能不提供和一般模板相同的接口。因此它往往拒绝在模板化基类内寻找继承而来的名称。
例如有一家公司只使用加密通信:
class CompanyZ
{
public:
void sendEncrypted(const std::string& msg);
};
之前定义的MsgSender模板对CompanyZ并不合适,因为该模板还提供了sendClear发送明文的函数。因此要针对CompanyZ产生一个MsgSender特化版:
template<>
class MsgSender<CompanyZ>
{
public:
void sendSecret(const MsgInfo& info)
{.........}
};
然而如果对于LoggingMsgSender模板传递的是CompanyZ 则会产生错误:
template<typename Company>
class LoggingMsgSender:public MsgSender<Company>
{
public:
void sendClearMsg(const MsgInfo& info)
{
sendClear(info); //如果Company = CompanyZ,这个函数不存在
}
};
解决办法:
1、在基类函数调用之前加上“this->”:
template<typename Company>
class LoggingMsgSender:public MsgSender<Company>
{
public:
void sendClearMsg(const MsgInfo& info)
{
this->sendClear(info); //成立,假设sendClear 将被继承
}
};
2、使用using声明:
template<typename Company>
class LoggingMsgSender:public MsgSender<Company>
{
public:
using MsgSender<Company>::sendClear; //告诉编译器,请它假设sendClear位于base class内
void sendClearMsg(const MsgInfo& info)
{
sendClear(info); //成立,假设sendClear将被继承下来
}
};
3、明确指出被调用的函数位于基类内:
template<typename Company>
class LoggingMsgSender:public MsgSender<Company>
{
public:
void sendClearMsg(const MsgInfo& info)
{
Msgsender<Company>::sendClear(info); //成立,假设sendClear将被继承下来
}
};
但这往往是最不让人满意的一个解法,因为如果被调用的是virtual函数,上述的明确资格修饰会关闭“virtual绑定行为”。