Effective C++ Item 43
2009-08-28


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&ltCompany>::sendClear;
        ...
        void sendSecretMsg(const MsgInfo& info)
        {
            ...
            senderClear(info);
            ...
        }
        ...
};

this を用いた基底クラスへのアクセス例。


template<typename Company>
class LoggingMsgSender: public MsgSender<Company>
{
    public:
        ...
        void sendSecretMsg(const MsgInfo& info)
        {
            ...
            MsgSender&ltCompany>senderClear(info);
            ...
        }
        ...
};

前回次回

[c++(マクロへ移行中)]

コメント(全0件)
コメントをする


記事を書く
 powered by ASAHIネット