Effective C++ Item 432009年08月28日 11時27分37秒

  • 派生クラステンプレート内で基底クラステンプレートにアクセスする場合は、「this->」 を使う、using で宣言する、または明示的な基底クラスを表記する方法がある。
  • 「template <>」は total template specialzation に使う。

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);
            ...
        }
        ...
};

前回次回