找出c+中两个对象类之间的继承关系+; 我有一个抽象C++基类CpLuin。从中可以直接或间接派生出许多类。现在给定CPlugin*a,*b,我需要找出,如果a的实类是从b的实类派生出来的

找出c+中两个对象类之间的继承关系+; 我有一个抽象C++基类CpLuin。从中可以直接或间接派生出许多类。现在给定CPlugin*a,*b,我需要找出,如果a的实类是从b的实类派生出来的,c++,reflection,inheritance,C++,Reflection,Inheritance,也就是说,我想这样做: void checkInheritance(CPlugin *a, CPlugin *b){ if (getClass(a).isDerivedFrom(getClass(b)){ std::cout << "a is a specialization from b's class" << std::endl; } } void checkheritation(CPlugin*a,CPlugin*b){ if(getClass(a

也就是说,我想这样做:

void checkInheritance(CPlugin *a, CPlugin *b){
  if (getClass(a).isDerivedFrom(getClass(b)){
    std::cout << "a is a specialization from b's class" << std::endl;
  }
}
void checkheritation(CPlugin*a,CPlugin*b){
if(getClass(a).isDerivedFrom(getClass(b)){

Std::P> Typeinfo和动态C:

< P>你不能在C++中这样做。在运行时获取有关类型的信息的唯一方法是RTTI。RTTI没有足够的能力来做你需要的。请解释你正在努力实现的,然后你会得到更好的答案。

< P>一个整体的解决方案真的很难提供。你在尝试什么?G是一个依赖于两个参数的具体类型的行为:这叫做双调度。几页现代C++设计(Andrei Alexandrescu)致力于这个主题。
一旦在一个代码点上知道这两个参数的实际具体类型,“isDerivedFrom”部分就可以用boost type_traits来回答:。

我真的不明白你在追求什么,但你可以用以下方式使用虚拟方法:

template <typename Derived>
struct TypeChecker
{
  virtual bool ParentOf(CPlugin const& c) const
  {
    return dynamic_cast<Derived const*>(&c);
  }
};
并使从
CPlugin
派生的每个类也从
TypeChecker
继承:

class SomePlugin: public CPlugin, private TypeChecker<SomePlugin> {};
class-SomePlugin:public-CPlugin,private-TypeChecker{};
最后像这样使用它:

void checkInheritance(CPlugin const& lhs, CPlugin const& rhs)
{
  if (!rhs.ParentOf(lhs)) return;

  std::cout << "lhs is derived from rhs' class\n";
}
void checkheritation(CPlugin const&lhs,CPlugin const&rhs)
{
如果(!rhs.ParentOf(lhs))返回;

std::cout您可以使用动态强制转换来测试对象是否属于编译时已知类型的子类型。根据对象的运行时类型更改行为的机制是虚拟函数,它为您提供了编译时已知接收器类型的作用域

因此,您可以通过一个虚拟函数来实现相同的效果,这样您就可以在编译时将类型放在一边,然后动态强制转换以根据该类型检查另一边:

#include <iostream>

class Plugin {
    public:
    virtual bool objectIsDerivedFromMyClass ( const Plugin & object ) const = 0;
};

template <typename T, typename BasePlugin = Plugin>
class TypedPlugin : public BasePlugin {
    public:
    virtual bool objectIsDerivedFromMyClass ( const Plugin & object ) const {
        return dynamic_cast<const T*> ( &object ) != 0;
    }

    private:
        int CheckMe(const T*) const;
};

class PluginA : public TypedPlugin<PluginA> {};
class PluginB : public TypedPlugin<PluginB, PluginA> {};
class PluginC : public TypedPlugin<PluginC> {};

int main () {
    PluginA a;
    PluginB b;
    PluginC c;

    std::cout << std::boolalpha
    << "type of a is derived from type of a " <<  a.objectIsDerivedFromMyClass ( a ) << '\n'
    << "type of a is derived from type of b " <<  b.objectIsDerivedFromMyClass ( a ) << '\n'
    << "type of b is derived from type of a " <<  a.objectIsDerivedFromMyClass ( b ) << '\n'
    << "type of c is derived from type of a " <<  a.objectIsDerivedFromMyClass ( c ) << '\n'
    ;

    return 0;
}
#包括
类插件{
公众:
虚拟布尔对象由MyClass(const插件和对象)驱动,const=0;
};
模板
类TypedPlugin:PublicBasePlugin{
公众:
虚拟布尔对象由MyClass(常量插件和对象)常量驱动{
返回动态_cast(&object)!=0;
}
私人:
int CheckMe(常数T*)常数;
};
类PluginA:公共类型plugin{};
类PluginB:publicTypedPlugin{};
类PluginC:public-TypedPlugin{};
int main(){
PluginA;
PluginB;
plugc;

STD:一般来说,C++中需要重新考虑这样一种设计。为什么需要检查一个代码< CPlugin < /C> >是从另一个< CPlugin >代码>派生的?我不明白为什么使用代码< CPlugin > /COD>代码是必要的。只有这样才能实现这个<代码> GET类
自己操作(即类型的基本类型反射功能)。这很快就会变得单调乏味(而且肮脏)特别是如果多个继承人开始玩……是的,正如上面所说的,如果我是你,我宁愿问自己为什么我需要知道这一点。你的目标是什么让你考虑<代码>签入< <代码>函数?Qt支持这个!!!我已经做了这件事。出于好奇,这个函数的实际使用是什么?oesn不允许确定
*b
的运行时类是否派生自
*a
的运行时类。Dynamic cast允许您确定@Olidynamic\u cast返回null或在两个类不兼容时抛出异常。它实际上不会告诉您哪个是父类,哪个是子类,但如果您知道w哪一个是基类,然后你可以使用dynamic_cast检查它是否是一个孩子。我可能弄错了,有人能证实这一点吗?@Cisco:OP没有类,他有对象。给定
CPlugin*a,*b
,你不能使用
dynamic_cast
做任何有用的事情(因为除了
CPlugin
,您没有任何编译时类型).@ICECROBIN:
is_base_of
是一个编译时构造-它不适用于任意运行时指针。当然,这就是为什么我认为这个问题有两个方面:实现双重分派以获得两个参数的具体类型,以及使用boost::is_base_of验证继承关系。我将编辑我对clarit的回答哎哟,你跑得这么快,我哪里能抱怨呢?:)我梦想着有一天,当我发布第一个答案或评论时。很抱歉,我看不出我的推理有什么问题。双重分派将允许OP有一个单一的函数/方法,其中参数将以各自的具体类型传递。从代码的这一点来看,我看不出调用是如何的_base_of是一个issU.@ ICECIMIM:没有一个函数,每个类必须有一个(或者可能是每个类的组合)。“OLI”可能是因为C++中可以这样做,但是像大多数事情一样,C++不为你做这件事,你必须自己做。
#include <iostream>

class Plugin {
    public:
    virtual bool objectIsDerivedFromMyClass ( const Plugin & object ) const = 0;
};

template <typename T, typename BasePlugin = Plugin>
class TypedPlugin : public BasePlugin {
    public:
    virtual bool objectIsDerivedFromMyClass ( const Plugin & object ) const {
        return dynamic_cast<const T*> ( &object ) != 0;
    }

    private:
        int CheckMe(const T*) const;
};

class PluginA : public TypedPlugin<PluginA> {};
class PluginB : public TypedPlugin<PluginB, PluginA> {};
class PluginC : public TypedPlugin<PluginC> {};

int main () {
    PluginA a;
    PluginB b;
    PluginC c;

    std::cout << std::boolalpha
    << "type of a is derived from type of a " <<  a.objectIsDerivedFromMyClass ( a ) << '\n'
    << "type of a is derived from type of b " <<  b.objectIsDerivedFromMyClass ( a ) << '\n'
    << "type of b is derived from type of a " <<  a.objectIsDerivedFromMyClass ( b ) << '\n'
    << "type of c is derived from type of a " <<  a.objectIsDerivedFromMyClass ( c ) << '\n'
    ;

    return 0;
}