网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> VC编程 >> 文章正文
  编译期判断类的继承性            【字体:
编译期判断类的继承性
作者:佚名    文章来源:不详    点击数:    更新时间:2007-9-12    

介绍一个雕虫小技:编译期判断类的继承性。
正在装载数据……
具体来说就是类型
U是否继承自类型T。该技术的灵感和源头来自Andrei Alexandrescue的《Modern C++ Design》。原书中描述的此技术有一个小小的bug

源代码

template <class T , class U>

class __conversion

{

   static char test(U);

   static double test(...);

   static T maket();

public:

   enum{exists = sizeof(test(maket())) == sizeof(char) };

   enum{exists2way = exists && __conversion<U,T>::exists};

   enum{sametype = false};

};

 

 

template <class T>

class __conversion<T,T>

{

public:

   enum{exists = 1};

   enum{exists2way = 1};

   enum{sametype = 1};

};

 

 

//判断T是否是U的父类或者TU是相同类型

#define SUPERSUBCLASS(T,U) (kimi_boost::__conversion<const U*,const T*>::exists &&\

   !kimi_boost::__conversion<const T*,void*>::sametype)

 

//判断T是否是U的父类

#define SUPERSUBCLASS_STRICT(T,U) (SUPERSUBCLASS(T,U) &&\

   !kimi_boost::__conversion<const T, const U>::sametype)

测试程序

class a{};

class b : public a{};

void supersub_test()

{

   using std::cout;

   using std::endl;

 

   cout<<SUPERSUBCLASS(int,unsigned)<<endl;

   cout<<SUPERSUBCLASS(int,int)<<endl;

   cout<<SUPERSUBCLASS_STRICT(int,int)<<endl;

   cout<<SUPERSUBCLASS(std::forward_iterator_tag,std::random_access_iterator_tag)<<endl<<endl;

 

   cout<<SUPERSUBCLASS(a,a)<<endl;

   cout<<SUPERSUBCLASS(a,b)<<endl;

   cout<<SUPERSUBCLASS(b,b)<<endl;

   cout<<SUPERSUBCLASS(b,a)<<endl<<endl;

 

   cout<<SUPERSUBCLASS_STRICT(a,a)<<endl;

   cout<<SUPERSUBCLASS_STRICT(a,b)<<endl;

   cout<<SUPERSUBCLASS_STRICT(b,b)<<endl;

   cout<<SUPERSUBCLASS_STRICT(b,a)<<endl;

 

}

程序结果

0

1

0

1

 

1

1

1

0

 

0

1

0

0

 


本文来源:http://blog.csdn.net/laibach0304/archive/2007/08/24/1758095.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     directx 图形接口指南(…
     win2k下的api函数的拦截
     用crypto  api  实现公钥…
     根据别人的md5源码封装的…
     vc中使用gdi+合并jpg图片
     document/view的交互 --…
     windows下的函数hook技术
     windows api函数大全一
     用vc 6.0实现串行通信的…
     vc++技术内幕(第四版)…
  • 保留weblogic 中jsp编译后生…

  • Java源文件的编译、下载、解…

  • 使用ICE遇到的编译问题

  • java类的反射机制

  • Solaris10下,使用SunStudio…

  • Ajax - javascript之实现…

  • 编译器的函数名修饰规则

  • VB.NET类的总结(一)

  • Delphi6编译错误大全

  • 访问祖先类的虚方法

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David