简体   繁体   English

如何从接口获取派生类?

[英]How do I get the derived class from a interface?

While trying to create an Entity-Component-System in C++, I have faced some problems regarding by lack of knowledge on the language. 在尝试用C ++创建一个Entity-Component-System时,由于缺乏对该语言的了解,我遇到了一些问题。

With a class Entity , that holds the interface IComponent (which acts more like a flag that says "I hold data"), I have a method Add that adds a Component to the Entity if there is not another IComponent of the same class already in it. 对于拥有实体类的Entity ,它拥有接口IComponent (其行为更像是表示“我拥有数据”的标志),我有一个方法Add ,如果已经没有同一个类的另一个IComponent,则将Component 添加到Entity。它。

Here's an oversimplified sample code: 这是一个过于简化的示例代码:

struct IComponent{};

struct Foo : IComponent{ int x;};
struct Bar : IComponent{ int y; };

class Entity{
    vector<IComponent*> entityComponents;

    void Add(IComponent* componentToAdd){
        if("entityComponents" does not contain the class of "componentToAdd")
            entityComponents.add (componentToAdd)

    }
}

My expected result would be 我的预期结果是

Entity e;
Foo f;
Bar b;
Foo anotherF;

e.Add(f); // Works
e.Add(b); // Works
e.Add(anotherF); // Does not work because another 
                 //item in the array already inherits Foo

But I do not know how to get the base class of Foo and Bar from inside the IComponents list and check if they are repeated. 但是我不知道如何从IComponents列表中获取Foo和Bar的基类,并检查它们是否重复。

How may I get them? 我怎样才能得到它们? How may I cast an IComponent to a Foo if Foo is in the IComponent list? 如果Foo在IComponent列表中,如何将IComponent投射到Foo?

Checkout dynamic_cast . 签出dynamic_cast You can attempt to cast a pointer to base class to a pointer to a derived class. 您可以尝试将指向基类的指针转换为指向派生类的指针。 It fails if the instantiated object is not of type derived and in this case returns null. 如果实例化的对象不是派生类型,则失败,在这种情况下返回null。

As Bar Stool stated, my solution was 正如Bar Stool所说,我的解决方案是

template<typename T>
bool HasComponent(){
  for(Component* component: this->components)
        if(T* casted = dynamic_cast<T*>(component))
                return true;           
  return false;

}

And later just check if "HasComponent()" is false and then add it 然后稍后检查“ HasComponent()”是否为假,然后将其添加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果派生类有一个具有相同名称的方法,如何从派生类调用基类方法? - how do I call a base class method from the derived class, if the derived class has a method with the same name? 如何从派生类访问引用成员变量? - How do I access reference member variable from derived class? 我如何确保得到派生类方法调用? - How do I make sure I get the derived class method called? 如何转发声明从转发声明的模板库 class 派生的 class? - How do I forward declare a class derived from a forward declared template base class? 如何将基类指针转换为派生自该类的指针? C ++ - How do I cast a base class pointer to a pointer of a class derived from it? C++ 从常量引用接口向上转换到派生类 - Upcast from Const Reference Interface to Derived Class 如何在派生类中调用&lt;&lt;的基类运算符 - How do I call << operator of base class in the derived class 如何从派生自Nan :: ObjectWrap的类返回本机对象? - How do I return a native object from a class derived from Nan::ObjectWrap? 如何从派生类中调用在派生类中重载的模板类函数? - c++ How do you call a template class function that is overloaded in the derived class, from the derived class? 我如何确定在C ++中运行时从特定类派生了多少个类 - How do I figure out how many classes are being derived from a particular class at runtime in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM