![]() |
|
||||||||||||||
| | 网站首页 | 数据库教程 | web编程 | 服务器 | 程序设计 | | ||
|
||
|
||||||
| 编译期判断类的继承性 | ||||||
作者:佚名 文章来源:不详 点击数: 更新时间:2007-9-12 ![]() |
||||||
|
介绍一个雕虫小技:编译期判断类的继承性。 源代码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的父类或者T和U是相同类型 #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 | ||||||
| 【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 | ||||||
| 网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!) |
| | 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 | 网站公告 | 网站地图 | 管理登录 | | |||
|