C++中利用模板实现instanceof和is_super_of

最近看了一点模板元编程的东西,忽然灵光一闪,便想出了一个方法来判断一个类是否是另一个类的基类is_base_of。稍加改变也可以实现类似于java的instanceof

虽然平时写程序很少会需要这两个东西,不过万一用到呢

 

预备知识:

1. 如果将一个T类型临时对象传递给一个函数,那么这个函数接受的参数类型必须是:const T& 或 T 

2.  函数模板匹配的原则:见http://www.cnblogs.com/hdtianfu/archive/2011/09/05/2167973.html


利用上述两点,可以制作出下面这个模板函数 is_supe_of<T1, T2>()

注意:这个函数有一点缺陷: T2必须具有缺省构造函数。

代码如下:

 1 template<typename T>
 2 class CTestSuper{
 3 public:
 4 
 5     static bool testsuper(const T&){return true;}
 6     template<typename U>
 7     static bool testsuper(U&){return false;}
 8 };
 9 
10 template<typename T1, typename T2>
11 bool is_super_of(){
12 
13     return CTestSuper<T1>::testsuper(T2());
14 }
15 
16 template<typename T1, typename T2>
17 bool instanceof(const T2&){
18     return CTestSuper<T1>::testsuper(T2());
19 } 


注:instanceof 的实现是有缺陷的, 它只能按照静态类型进行判定,如果想要判断一个基类指针是否指向某个子类,这种方法便无能为力了,这时你需要的是dynamic_cast。 

 

测试代码如下: 

 1 class A{};
 2 class B : public A{};
 3 class C:public B{};
 4 
 5 int main(){
 6 
 7     cout << "A is Super of B : " << (is_super_of<A, B>() ? "true" : "false") << endl;
 8     cout << "A is Super of C : " << (is_super_of<A, C>() ? "true" : "false") << endl;
 9     cout << "B is Super of A : " << (is_super_of<B, A>() ? "true" : "false") << endl;
10     cout << "C is Super of A : " << (is_super_of<C, A>() ? "true" : "false") << endl;
11 
12     B b;
13     cout << "b is instance of A : " << (instanceof<A>(b) ? "true" : "false") << endl;
14     cout << "b is instance of B : " << (instanceof<B>(b) ? "true" : "false") << endl;
15     cout << "b is instance of C : " << (instanceof<C>(b) ? "true" : "false") << endl;
16 
17 }

 

输出如下:

A is Super of B : true
A is Super of C : true
B is Super of A : false
C is Super of A : false
b is instance of A : true
b is instance of B : true
b is instance of C : false


posted on 2013-06-06 17:05  sanlo  阅读(3017)  评论(0编辑  收藏  举报