如何测试类B是否派生自类A? [英] How to test whether class B is derived from class A?

查看:149
本文介绍了如何测试类B是否派生自类A?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更具体地说,假设我有一个具有参数 A B 的类模板,有一个编译器错误(当模板被实例化时)如果B不是从A派生的。

More specifically, let's say I have a class template with parameters A and B, and I would like to have a compiler error (when the template is being instantiated) if B is not derived from A.

template<class A, class B>
class Foo
{
    // static_assert(B is derived from A)
};


推荐答案

它很简单我会再次发布的解决方案:

This has been asked so very many times before, but it's so simple I'll post the solution again:

~Foo()
{
    A* p = (B*)0; // B is required to be a subtype of A
}

这篇关于如何测试类B是否派生自类A?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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