【问题标题】:Returning a static cast to a pointer doesn't return a pointer C++将静态转换返回到指针不会返回指针 C++
【发布时间】:2022-11-21 14:08:04
【问题描述】:

在“实体”类中,有一个函数将组件类型名作为参数,并且应该返回一个指针如果在组件数组中找到该组件。相反,它只返回组件的副本,而不是指针,尽管这样做:

return static_cast<T*>(ptr)

这是相关代码:

云服务器h(只有必要的代码)。

inline ComponentTypeId getUniqueComponentID() {
    static ComponentTypeId lastID = 0u;
    return lastID++;
}

template <typename T> inline ComponentTypeId getComponentTypeID() noexcept {
    static_assert(std::is_base_of<Component, T>::value, "Failed at getComponentTypeID/static_assert() --> ECS/ECS.h");
    static const ComponentTypeId typeID = getUniqueComponentID();
    return typeID;
}

// Base "Component" class
class Component {
    // Code
};

// Base "Entity" class
class Entity {
    private:
        ComponentArray compArr;
        ComponentBitset compBitset;
        std::vector<std::unique_ptr<Component>> components;

        bool active = true;

    public:
        Entity() {}
        virtual ~Entity() {}

        template<typename T> bool hasComponent() const {
            // Returns the bitset (bool value)
            return compBitset[getComponentTypeID<T>()];
        }

        template<typename T> T& getComponent() const {
            // Returns pointer to component
            return *static_cast<T*>(compArr[getComponentTypeID<T>()]);
        }

        void update() {
            // Goes through all the components (for this entity) calls their update method
            for(auto &c : components) c->update();
        }

        void draw() {
            // Goes through all the components (for this entity) calls their draw method
            for(auto &c : components) c->draw();
        }

        inline bool isActive() {return active;}
        void destroy() {active = false;}
};

【问题讨论】:

  • *我忘记在示例的第 4 行包含一个分号,但这并没有改变问题,因为它存在于代码中。
  • 您的函数返回一个引用,而不是一个指针。但是,它不会制作副本。请说明您实际使用该功能的方式以及存在的问题。同时将您的代码缩减为 minimal reproducible example。显示的用法不起作用,因为您的函数不返回指针,而是返回引用。 static_cast&lt;T*&gt;(ptr) 是一个指针,添加 * 将取消引用该指针。
  • @AviBerger 该函数通过引用返回。在显示的代码中没有明显的复制点。
  • @user17732522,是的。我没有读过帖子的前几行到完整功能代码所在的位置。
  • 即使将 *static_cast&lt;T*&gt;(ptr) 更改为 static_cast&lt;T*&gt;(ptr) 也无法解决编译错误。这是错误:`在文件中包含从 ./src/ECS/components.h:11:0,从 ./src/game.cpp:7: ./src/ECS/hitbox_component.h: 在成员函数 'virtual void HitboxComponent::init()': ./src/ECS/hitbox_component.h:25:23: 错误:无法在赋值中将'TransformComponent' 转换为'TransformComponent*' transform = parent->getComponent<TransformComponent>(); Makefile:2: 目标 'build' 的配方失败 make: *** [build] Error 1 `

标签: c++ function oop pointers static-cast


【解决方案1】:

结果我在 getComponent() 函数中返回了一个引用而不是一个指针。

template<typename T> T& getComponent() const {
    // Returns pointer to component
    return *static_cast<T*>(compArr[getComponentTypeID<T>()]);
}

// Needs to be
template<typename T> T* getComponent() const {
    // Returns pointer to component
    return static_cast<T*>(compArr[getComponentTypeID<T>()]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多