zoukankan      html  css  js  c++  java
  • 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


  • 相关阅读:
    CF1093F Vasya and Array
    CF1093D Beautiful Graph
    mysql主主同步
    mysql主从机制的部署与应用
    什么是多项式?
    从线性逼近到多项式逼近:泰勒级数
    机器学习--boosting家族之XGBoost算法
    倾情大奉送--Spark入门实战系列
    [机器学习笔记] 什么是分类,什么是回归?
    kafka中处理超大消息的一些考虑
  • 原文地址:https://www.cnblogs.com/hdtianfu/p/3121581.html
Copyright © 2011-2022 走看看