用template来判断类继承的关系

来源:互联网 发布:最优化牛顿法迭代例题 编辑:程序博客网 时间:2024/05/10 10:56

能自动判断一个类是不是从另外一个类继承下来的:

template<typename T , typename TBase> class TIsDerived
{
        public:
        static int t(TBase* base)
         {
                   return 1;
          }
         static  char t(void* t2)
         {
                    return 0;
          }

           enum
            {
                    Result = ( sizeof(int) == sizeof(t( (T*)NULL) )  ),
            };
};

用途:

bool   AISDerviedFromB  =  TIsDerived<ClassA, ClassB>::Result ;
如果ClassA是从ClassB派生的,那么 AISDerviedFromB   为true, 否则为false