Chinaunix首页 | 论坛 | 博客
  • 博客访问: 92427793
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: C/C++

2008-04-15 21:31:05

  来源:赛迪论坛    作者:sixth

第二,你可以使用一个 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

...
};

阅读(277) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~