C++ のテンプレートクラスにて、基底クラスの関数が派生クラスで継承されるのを見越して、コンパイラが名前解決の時に基底クラスの関数を探さなくなる時がある。そのような場合に三つの回避策がある。
MsgSender の total specializatoin の例。
template<>
class MsgSender<CompanyZ>
{
public:
...
void sendSecret(const MsgInfo& info)
{...}
...
};
this を用いた基底クラスへのアクセス例。
template<typename Company>
class LoggingMsgSender: public MsgSender<Company>
{
public:
...
void sendSecretMsg(const MsgInfo& info)
{
...
this->senderClear(info);
...
}
...
};
基底クラスへ直接アクセスする例。
template<typename Company>
class LoggingMsgSender: public MsgSender<Company>
{
public:
...
using MsgSender<Company>::sendClear;
...
void sendSecretMsg(const MsgInfo& info)
{
...
senderClear(info);
...
}
...
};
this を用いた基底クラスへのアクセス例。
template<typename Company>
class LoggingMsgSender: public MsgSender<Company>
{
public:
...
void sendSecretMsg(const MsgInfo& info)
{
...
MsgSender<Company>senderClear(info);
...
}
...
};
コメントをする