您的位置:学习中国 推荐教程 C++C语言 正文
原作者:kuku 添加时间:2007-06-02 原文发表:2007-06-02 人气:6 来源:互联网

本文章共4060字,分3页,当前第2页,快速翻页:
 
-----------------------------------------------------------------------------------------------------------------------------------------------

本文提示:《如何访问模板化基类中的名字(2)》是本站编辑们为广大网友精选的实用文章,本文阐述了关于文章的相关理论,相对来说专业性强,但是本文只是针对于某个问题提出的见解与论述,未必能辐射到相关问题的方方面面,所以本文处理问题的方法仅仅为您提供一些参考。更多问题请查阅学习中国网其他栏目哦.

-----------------------------------------------------------------------------------------------------------------------------------------------


class MsgSender{ // MsgSender; the same as the
public: // general template, except
... // sendCleartext is omitted
void sendSecret(const MsgInfo& info)
{ ... }
};
  注意这个 class definition(类定义)开始处的 "template <>" 语法。它表示这既不是一个 template(模板),也不是一个 standalone class(独立类)。正确的说法是,它是一个用于 template argument(模板参数)为 CompanyZ 时的 MsgSender template(模板)的 specialized version(特化版本)。这以 total template specialization(完全模板特化)闻名:template(模板)MsgSender 针对类型 CompanyZ 被特化,而且这个 specialization(特化)是 total(完全)的——只要 type parameter(类型参数)被定义成了 CompanyZ,就没有剩下能被改变的其它 template's parameters(模板参数)。

  已知 MsgSender 针对 CompanyZ 被特化,再次考虑 derived class(派生类)LoggingMsgSender: template
class LoggingMsgSender: public MsgSender{
 public:
 ...
 void sendClearMsg(const MsgInfo& info)
 {
  write "before sending" info to the log;
  sendClear(info); // if Company == CompanyZ,
  // this function doesn't exist!
  write "after sending" info to the log;
 }
 ...
};
  就像注释中写的,当 base class(基类)是 MsgSender时,这里的代码是无意义的,因为那个类没有提供 sendClear function(函数)。这就是为什么 C++ 拒绝这个调用:它认可 base class templates(基类模板)可以被特化,而这个特化不一定提供和 general template(通用模板)相同的 interface(接口)。结果,它通常会拒绝在 templatized base classes(模板化基类)中寻找 inherited names(继承来的名字)。在某种意义上,当我们从 Object-oriented C++ 跨越到 Template C++,inheritance(继承)会停止工作。

  为了重新启动它,我们必须以某种方式使 C++ 的 "don't 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 领域。)
 
本文章更多内容<<上一页 - 1 - 2 - 3 - 下一页>>
本页地址
收藏到:[收藏夹] [百度搜藏] [新浪ViVi] [POCO网摘] [ 和讯网摘] [好哦网摘] [Google书签]
               
[搜狐网摘] [365Key网摘] [天极网摘] [我摘] [博采网摘] [igooi网摘]
相关文章

对象布局及多态探索之菱形结构虚继承
C++之父Bjarne谈C++中的STL模板
C++箴言:用成员函数模板接受兼容类型
探索C++的秘密之详解extern C
C++箴言:为类型信息使用特征类
创建可移植的64位应用程序代码
设计OutLook风格的工具栏
C++程序员必需的修养
如何编写异常安全的C++代码
在c++程序中重启自己的一种方法
C++对象布局及多态之虚成员函数调用
理解隐式接口和编译期多态
C++程序中导出Word文档简易方法
C++程序设计从零开始之语句
对象布局及多态实现之成员函数的调用
C++箴言:谨慎使用私有继承
C++编程人员容易犯的10个C#错误
C语言 编程实例
C++Win32APIReadFile()读取文件

相关评论


本文章所属分类:首页 推荐教程 C++C语言   C++C语言