如何从向量中获取真实的类实例名称



我有类:

class IComponent{};

我将其用作其他特定类的基类。

class First : public IComponent{public: void First();};   
class Second : public IComponent{public: void Second();};

我有一个类对象,它有一个向量,用于存储从 IComponent 继承的类。

vector<IComponent*> vectorOfComponents;

在此类中,我有一个将组件添加到矢量的模板方法。

template<typename comp>
void AddComponent() {
    comp * c = new comp();
    getComponents().push_back(c);
}

但是我也希望有可能获得特定类型的组件,所以我创建了该方法:

template<typename comp>
comp * GetComponent()
{
    comp component;
    for (int i = 0; i < GetComponentsCount(); i++)
        if (typeid(*getComponents()[i]).name() == typeid(component).name())
            return (comp*)getComponents()[i];
    return NULL;
}

当我稍后运行它时:

    if (obj.GetComponent<FirstClass>() != NULL)
        std::cout << "isn't nulln";
    else
        std::cout << "is nulln";
我知道 vector 保存了 IComponent 继承

类的推送实例,但它总是打印我为空,因为第一个 typeid() 向我显示 IComponent,第二个向我显示特定的继承组件。

如何从向量中获取特定类型?我尝试了多态性,但这不起作用,或者我做错了什么:

template<typename comp>
void AddComponent() {
    comp c = comp();
    IComponent * i = &c;
    getComponents().push_back(i);
}

typeid仅适用于多态类类型。要使类具有多态性,它需要一个virtual函数。此外,如果类对象可能由基类指针拥有,则必须virtual析构函数,此处可能就是这种情况。

所以,你应该使用,

class IComponent{
    virtual ~ IComponent() = default;
};

编辑:此外,type_info::name()返回一个 C 字符串指针,这是使用 == 无法比较的。使用动态库的程序可能会在不同的地址观察到相同的name字符串(尽管否则这是不寻常的)。改为比较type_info对象。此外,typeid可以将类型作为操作数;您无需创建对象即可使用它。所以,你可以做到,

if (typeid(*getComponents()[i]) == typeid(comp))

请注意,这将检查是否完全匹配的类型。若要查找派生对象(允许comp作为基类),请使用dynamic_cast

if (comp *p = dynamic_cast<comp *>(getComponents()[i])) {
    return p;
}

我建议您考虑另一种可能的解决方法。

因此,您可以将函数getType添加到基类中:

enum class Types {
    First,
    Second
};
class IComponent {
    public:
        virtual Types getType() const = 0;
        virtual ~IComponent() = default;
};

然后在派生类中实现它:

class First : public IComponent {
public:
    virtual Types getType() const override {
        return Types::First;
    }
};
class Second : public IComponent {
    public:
        virtual Types getType() const override {
            return Types::Second;
        }
};

之后,搜索和其他任务似乎很容易:

const auto it = std::find_if(std::cbegin(components), std::cend(components),
    [](auto& ptr) {
        return ptr->getType() == Types::Second;
    }
);

魔杖盒示例

确保IComponent至少有一个virtual方法(析构函数就足够了,在处理继承时无论如何都应该是虚拟的),然后使用 dynamic_cast 而不是 typeid

class IComponent{
public:
    virtual ~IComponent() = default;
};
template<typename ComponentType>
ComponentType* GetComponent()
{
    auto &comps = getComponents();
    for (auto *item : comps) {
        ComponentType *comp = dynamic_cast<ComponentType*>(item);
        if (comp)
            return comp;
    }
    return nullptr;
}

最新更新