gpt4 book ai didi

c++ - 如何将不同的对象存储在一个数组中,并得到某个类型的对象?

转载 作者:行者123 更新时间:2023-11-30 05:09:44 25 4
gpt4 key购买 nike

您可能听说过 Entity Component System ,其中所有内容都是一个 Entity,每个实体都有一个控制其功能的 Component 列表。

我正在尝试了解如何在数组中存储不同的对象(每个对象都继承 Component),并能够根据对象的类型从该数组中获取对象。

我能想到的第一个解决方案是为继承组件的对象类型设置一个枚举:

enum ComponentType : unsigned char    // There will never be more than 256 components
{
EXAMPLE_COMPONENT,
ANOTHER_EXAMPLE_COMPONENT,
AND_ANOTHER_EXAMPLE_COMPONENT
};

然后 Component 基类有一个 ComponentType 类型; 和一个 getter,每个子组件设置它的类型例如:

ExampleComponent::ExampleComponent()
{
type = EXAMPLE_COMPONENT;
}

然后我会有一个 GetComponent 函数:

Component* Entity::GetComponent(ComponentType type)
{
for (unsigned int i = 0; i < m_components.size(); i++)
{
if (m_components.at(i).GetType() == type)
{
return &m_components.at(i);
}
}

return nullptr;
}

// Note: m_components is an std::vector;

最后您将调用 GetComponent 例如:

(ExampleComponent*) component = entity.GetComponent(EXAMPLE_COMPONENT);

这样做的问题是,您需要为每种类型的组件分配一个enum,并且您还必须在使用GetComponent 后强制转换组件,以确保您可以访问它的自己的成员变量。

有没有人知道在不需要 enum 并且不需要强制转换组件的情况下执行此操作的正确方法?如果有一个解决方案仍然需要在每个组件中存储一个类型变量,那么它最好是一个字节,并且不能大于 4 个字节。

编辑:我也不想使用模板

提前致谢!

大卫

最佳答案

您的方法模拟多态性:拥有 type作为成员(member)和if检查该类型的语句通常表明要使用类层次结构。您已经声明要使用从 Component 派生的对象类型,所以你也应该适本地使用多态性。

您的方法中的第二个问题是您想要过滤“特定类型”,这或多或少等同于向下转换 — 即 dynamic_cast<>() : 当你通过某个ComponentTypeEntity::GetComponent() , 它返回指向 Component 的指针, 但该指针后面的对象始终是特定派生类的对象:在您的示例中,您总是得到一个 ExampleComponent对象,当你通过 EXAMPLE_COMPONENT到那个函数。

下面的问题很自然地出现了:你想用这个函数返回的对象做什么?您只能从 Component 调用方法接口(interface)/类,但派生类中没有方法!所以向下转型几乎没有意义(如果你返回一个指向从 Component 派生的类的对象的指针,它就会有意义。

这是使用多态性并在 getComponent() 中使用向下转换的样子方法,返回一个指向派生类的指针——请注意,该方法是一个模板,可以方便地为从 Component 派生的每个类实现它:

#include <string>
#include <vector>
#include <iostream>

class Component {
public:
virtual std::string getType() = 0;
};

using ComponentContainer = std::vector<Component*>;

class AComponent : public Component { public: virtual std::string getType() { return "A"; }; };
class BComponent : public Component { public: virtual std::string getType() { return "B"; }; };
class CComponent : public Component { public: virtual std::string getType() { return "C"; }; };


class Entity {
public:
template <typename T>
T* getComponent();

void putComponent(Component* c) { m_components.push_back(c); }

private:
ComponentContainer m_components;
};


template<typename T>
T* Entity::getComponent()
{
T* t = nullptr;
for (auto i : m_components) {
if ((t = dynamic_cast<T*>(i)) != nullptr)
break;
}

return t;
}

int main()
{
Entity e;
e.putComponent(new AComponent{});
e.putComponent(new BComponent{});

Component* c;
if ((c = e.getComponent<AComponent>()) != nullptr)
std::cout << c->getType() << std::endl;

// delete all the stuff
return 0;
}

大量使用dynamic_cast<>()从性能和设计的角度来看都是有问题的:它应该很少使用,如果有的话。

所以设计问题可能是所有东西都存储在一个容器里?您可以根据“行为”使用多个容器。由于行为在 ECS 中作为派生类或接口(interface)实现,因此 getComponent() - 此实体的类似方法只会返回某些(子)接口(interface)的对象。然后这些组件都将实现一个给定的接口(interface)方法,因此将消除向下转型的需要。

例如,假设您有“可绘制组件”,这表明层次结构:

// Drawable interface
class DrawableComponent : public Component {
public:
virtual void draw() const = 0;
};

// Drawable objects derive from DrawableComponent
class DComponent : public DrawableComponent {
public:
virtual void draw() const { /* draw the D component */ }
};

然后,一个实体可以有一个容器 DrawableComponent对象,你只需遍历这些对象并调用 draw()在每个:

using DrawableContainer = std::vector<DrawableComponent*>;
// m_drawables is a memober of Entity with above type
const DrawableContainer& Entity::getDrawables() { return m_drawables; }

// then just draw those objects
for (auto d : entity.getDrawables())
d->draw(); // no downcast!

关于c++ - 如何将不同的对象存储在一个数组中,并得到某个类型的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46004521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com