条款43:学习处理模板化基类内的名称

问题代码

#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绑定行为”。
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值