Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > 模板vs虚函数
【标  题】:模板vs虚函数
【关键字】:vs
【来  源】:BLOG.CSDN.NET

模板vs虚函数

Your Ad Here

考虑如下两个容器的定义:

 

 

template <typename T>

class Math_container {

public:

   

         size_t size() const

         {

                   // trivial implemention

                   return 0;

         }

 

 

         const T& operator[] (size_t i) const

         {

                   // trivial implemention

                   static T data;

                   return data;

         }

 

 

         bool operator==(const Math_container& a) const

         {

                   if (size() != a.size())

                            return false;

                   for (int i = 0; i < size(); ++i)

                            if ((*this)[i] != a[i])

                                     return false;

                   return true;

         }

 

 

};

 

 

template <typename T>

class Music_container {

public:

   

         size_t size() const

         {

                   // trivial implemention

                   return 0;

         }

 

 

         const T& operator[] (size_t i) const

         {

                   // trivial implemention

                   static T data;

                   return data;

         }

 

 

         bool operator==(const Music_container& a) const

         {

                   if (size() != a.size())

                            return false;

                   for (int i = 0; i < size(); ++i)

                            if ((*this)[i] != a[i])

                                     return false;

                   return true;

         }

};

 

 

两个类的operator==是一样的,没有必要存在两份完全一样的定义,所以应该抽取出来放在共同的基类中,然而基类operator==的实现依赖于派生类的某些成员函数,解决这种问题的典型做法是使用虚函数:在基类中定义虚函数,在派生类中改写。

 

 

template <typename T>

class Basic_ops

{

public:

 

 

         virtual size_t size() const = 0;

         virtual const T& operator[] (size_t i) const = 0;

 

 

         bool operator==(const Basic_ops& a) const

         {

                   if (size() != a.size())

                            return false;

                   for (int i = 0; i < size(); ++i)

                            if (operator[](i) != a[i])

                                     return false;

                   return true;

         }

};

 

 

template <typename T>

class Math_container : public Basic_ops<T> {

public:

   

         size_t size() const

         {

                   // trivial implemention

                   return 0;

         }

 

 

         const T& operator[] (size_t i) const

         {

                   // trivial implemention

                   static T data;

                   return data;

         }

 

 

};

 

 

template <typename T>

class Music_container : public Basic_ops<T> {

public:

   

         size_t size() const

         {

                   // trivial implemention

                   return 0;

         }

 

 

         const T& operator[] (size_t i) const

         {

                   // trivial implemention

                   static T data;

                   return data;

         }

};

 

 

但是虚函数有一个最大的问题被调用的函数只有在执行期间才能确定下来。如果能在编译期间就确定下来,执行速度肯定会有所提高。为了做到这一点,基类必须知道派生类的类型信息,这一点可以通过将派生类的类型作为模板参数传递给基类来实现。

 

 

template <typename C>

class Basic_ops

{

public:

 

 

         const C& derived() const

         {

                   return static_cast<const C&>(*this);

         }

 

 

         bool operator==(const C& a) const

         {

                   if (derived().size() != a.size())

                            return false;

                   for (int i = 0; i < derived().size(); ++i)

                            if (derived()[i] != a[i])

                                     return false;

                   return true;

         }

};

 

 

template <typename T>

class Math_container : public Basic_ops<Math_container<T> > {

重学C++(1)——几个常常忽视的简单的类的问题:【上一篇】
抓狂的Namespace Shell Extension (8):【下一篇】

【相关文章】
  • WinCVS + CVSNT构建介绍
  • WinCVS + CVSNT构建介绍
  • vs.net2005 beta2 + Microsoft Enterprise Library June 2005
  • 技术的SONY vs 朴实的任天堂
  • vs.net2003+WSE2.0 打造安全的webservice
  • vs.net2005 beta2与wse2.0
  • vs.net2005beta2 与 vs.net2003的区别
  • 向VSTO 2005 迁移时,将 VBA 代码转换为 Visual Basic .NET
  • 关于E17 CVS0926版本中图标混乱的解决方法
  • 在VS2005 正确地创建、部署和维护由1.1迁移到ASP.NET 2.0 应用程序注意事项
  • 【随机文章】
  • 自动化播出系统-数字音频网的建设
  • Zero-copy sniffer
  • 《平衡》(Ballance)最终完美硬盘版, eMule下载
  • 数据流量监测工具[视频会议][源玛共享]
  • 用数组实现flash中的迷宫行走
  • FVWM的配置文件(王垠的)
  • 个人电脑防御黑客绝招
  • Visual Basic Orcas:下一代的VB
  • Freebsd 下 MOUNT用法
  • 引导程序的编写
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.