static_cast从基类指向派生类指针是无效的

soo*_*kie 1 c++ pointers static-cast

我正在创建一个简单的测试实体组件系统.我有一个基Component类与几个派生类.然后我有几个系统将一些逻辑应用于这些组件.

// Component.h
// ------------
class Component
{
public:
    Component();
    ~Component();
}


// ControlComponent.h
// -------------------
#include <string>
#include "Component.h"

class ControlComponent : public Component
{
public:
    std::string input = ""; // store simple input instruction
    ControlComponent();
    ~ControlComponent();
};


// ControlSystem.cpp
void ControlSystem::update(Entity* entity)
{
    vector<Component*>* components = entity->getComponents();

    for (Component* component : *components)
    {
        PositionComponent* pc = static_cast<PositionComponent*>(component);
        ControlComponent* cc = static_cast<ControlComponent*>(component);

        if (pc != nullptr && cc != nullptr)
        {
            std::cout << "Which direction would you like to go?" << std::endl;
            std::string input;
            std::cin >> input;
            cc->input = input; // application breaks here

            // Apply some logic...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我static_cast从基础Component*到任何一个派生组件(PositionComponent*ControlComponent*)并且两个结果都不是nullptr(即转换成功)时,我得到无效的值,就像cc->input无法从字符串中读取字符等.

我在我的实体工厂中连接组件,如下所示:

void EntityFactory::wireUpPlayer(Entity* player)
{
    player->addComponent(new HealthComponent());
    player->addComponent(new ControlComponent());
    player->addComponent(new PositionComponent());
}
Run Code Online (Sandbox Code Playgroud)

addComponent的实现如下:

void Entity::addComponent(Component* component)
{
    m_components.push_back(component);
}
Run Code Online (Sandbox Code Playgroud)

显示这些组件具有有效的内存地址,因此我不确定问题的来源.

Jos*_*ley 5

当我static_cast从基础Component*到派生组件(PositionComponent*ControlComponent*)中的任何一个并且两个结果都不是nullptr(即转换成功)时......

当从基类转换到派生类时,static_cast它告诉编译器,“相信我,我知道我在做什么。” 换句话说,如果它甚至可能合法,它就会“成功”并返回 non- nullptr。如果在运行时它是不合法的,您将获得未定义的行为,尝试使用一个类的实例,就像它是另一个类的实例一样。

使用dynamic_cast来代替。


Pet*_*ker 5

static_cast不会在运行时检查有效性; 如果转换编译,它在运行时假定转换是可以的.如果您没有转换空指针,则a的结果static_cast将不是空指针.要获得一个需要检查的强制dynamic_cast转换,反过来,需要将指针转换为指向多态类型,即具有至少一个虚函数的类型.这意味着要更改Component为至少拥有一个虚拟功能.

  • 由于“Component”是从中派生的,因此使 d'tor 虚拟化几乎总是正确的做法。 (2认同)