获取类层次结构中类型的基类 [英] Get base class for a type in class hierarchy

查看:138
本文介绍了获取类层次结构中类型的基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在类层次结构中获取基类类型?

Is is possible to get base class type in a class hierarchy?

例如:

struct A{};
struct B{} : public A;
struct C{} : public B;

我想要一个具有 typedef基础< T> :: Type的模板里面这样:

I want some template that will have typedef Base<T>::Type inside like this:

Base<A>::Type == A
Base<B>::Type == A
Base<C>::Type == A

这是可能吗?在我有多重继承的情况下呢?

Is this possible? What about the case when I have multiple inheritance?

推荐答案

我认为 std :: is_base_of 可以帮助您

I think std::is_base_of can help you

#include <type_traits>

std::is_base_of<B, D>()




如果D派生自B或者两者都是相同的非联合类,则
提供等于true的成员常量值。否则值为
false。

If D is derived from B or if both are the same non-union class, provides the member constant value equal to true. Otherwise value is false.

您可以使用它来检查类是否是另一个类的基类: / p>

You can use it to check if a class is base class of another or not :

std::is_base_of<A, A>()   // Base<A>::Type == A

std::is_base_of<A, B>()   // Base<B>::Type == A

std::is_base_of<A, C>()   // Base<C>::Type == A

这篇关于获取类层次结构中类型的基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆