C++箴言:如何访问模板化基类中的名字(二)

2014-11-24 13:16:36 · 作者: · 浏览: 20
同的 interface(接口)。结果,它通常会拒绝在 templatized base classes(模板化基类)中寻找 inherited names(继承来的名字)。在某种意义上,当我们从 Object-oriented C++ 跨越到 Template C++,inheritance(继承)会停止工作。

  为了重新启动它,我们必须以某种方式使 C++ 的 "dont look in templatized base classes"(不在模板基类中寻找)行为失效。有三种方法可以做到这一点。首先,你可以在调用 base class functions(基类函数)的前面加上 "this->":

template
class LoggingMsgSender: public MsgSender {
public:
...

void sendClearMsg(const MsgInfo& info)
{
 write "before sending" info to the log;
 this->sendClear(info); // okay, assumes that
 // sendClear will be inherited
 write "after sending" info to the log;
}
...
};

  第二,你可以使用一个 using declaration,如果你已经读过《 C++箴言:避免覆盖通过继承得到的名字》,这应该是你很熟悉的一种解决方案。该文解释了 using declarations 如何将被隐藏的 base class names(基类名字)引入到一个 derived class(派生类)领域中。因此我们可以这样写 sendClearMsg:

template
class LoggingMsgSender: public MsgSender {
public:
 using MsgSender ::sendClear; // tell compilers to assume
 ... // that sendClear is in the
 // base class
 void sendClearMsg(const MsgInfo& info)
 {
  ...
  sendClear(info); // okay, assumes that
  ... // sendClear will be inherited
 }
 ...
};

  (虽然 using declaration 在这里和《 C++箴言:避免覆盖通过继承得到的名字》中都可以工作,但要解决的问题是不同的。这里的情形不是 base class names(基类名字)被 derived class names(派生类名字)隐藏,而是如果我们不告诉它去做,编译器就不会搜索 base class 领域。)

  最后一个让你的代码通过编译的办法是显式指定被调用的函数是在 base class(基类)中的:

template
class LoggingMsgSender: public MsgSender {
public:
...
void sendClearMsg(const MsgInfo& info)
{
 ...
 MsgSender ::sendClear(info); // okay, assumes that
 ... // sendClear will be
} // inherited

...
};

  通常这是一个解决这个问题的最不合人心的方法,因为如果被调用函数是 virtual(虚拟)的,显式限定会关闭 virtual binding(虚拟绑定)行为。

  从名字可见性的观点来看,这里每一个方法都做了同样的事情:它向编译器保证任何后继的 base class template(基类模板)的 specializations(特化)都将支持 general template(通用模板)提供的 interface(接口)。所有的编译器在解析一个像 LoggingMsgSender 这样的 derived class template(派生类模板)是,这样一种保证都是必要的,但是如果保证被证实不成立,真相将在后继的编译过程中暴露。例如,如果后面的源代码中包含这些,

LoggingMsgSender zMsgSender;
MsgInfo msgData;
... // put info in msgData
zMsgSender.sendClearMsg(msgData); // error! wont compile

  对 sendClearMsg 的调用将不能编译,因为在此刻,编译器知道 base class(基类)是 template specialization(模板特化)MsgSender ,它们也知道那