首页 > 解决方案 > 虚函数调用未找到最派生的实现

问题描述

我正在尝试实现多态性,因此以后可以通过最少的重构来扩展我的项目。我也在尝试实现本书中的命令模式(https://gameprogrammingpatterns.com/command.html)。

期望的结果:我最衍生的虚函数类实现是通过std::shared_ptr<Base>(Most_Derived_Class). 如果这是错误的语法,我想通过一个指向其基类之一的共享指针来处理我最派生的类。

当前结果:通过期望结果中提到的指针调用虚函数的基类实现。

这是我的代码的可运行的简化版本:

#include <iostream>
#include <vector>
class Base
{
public:
    Base()
    {

    }

    void virtual update()
    {
        std::cout << "no update function\n";
    }
    void virtual draw()
    {
        std::cout << "no draw function\n";
    }
};

class Derived : public Base
{
public:
    Derived();
    void draw() override;
    void update() override;
};

Derived::Derived()
{
    
}

void Derived::draw()
{
    std::cout << "Derived Draw Called\n";
}

void Derived::update()
{
    std::cout << "Derived Update Called\n";
}

class Second_Derived : public Derived
{
public:
    Second_Derived() {}
    virtual void move_up() { std::cout << "Second_Derived Move_Up called\n"; };
};

class Third_Derived : public Second_Derived
{
public:
    Third_Derived();
    ~Third_Derived();

    void move_up() override;

    void draw() override;
    void update() override;
};

Third_Derived::Third_Derived() {}
Third_Derived::~Third_Derived() {}

void Third_Derived::move_up()
{
    std::cout << "Third Derived Move_Up called\n";
}

void Third_Derived::draw()
{
    std::cout << "Third Derived draw called\n";
}

void Third_Derived::update()
{
    std::cout << "Third Derived update Called\n";
}

class Command
{
public:
    virtual void execute(Second_Derived s_derived) { std::cout << "Base Command execute called\n"; }
};

class Move_Up : public Command
{
public:
    Move_Up() {}

    void execute(Second_Derived s_derived) override
    {
        std::cout << "Move_Up called\n";
        s_derived.move_up();
    }
};

class Input_Handler
{
public:
    Input_Handler() {};
    Command* handle_input()
    {
        return &return_command;
    }

private:
    Move_Up return_command;
};


class Caller
{
public:
    Caller();

    void update();
    void draw();

    void reg_obj(std::shared_ptr<Second_Derived> obj);
private:
    void process_inputs();
    Input_Handler i_handler;
    std::vector<std::shared_ptr<Second_Derived>> all_objects;
};

Caller::Caller()
{
    Third_Derived third_derived_class{};
    reg_obj(std::make_shared<Second_Derived>(third_derived_class));
}

void Caller::process_inputs()
{
    Command* current_inputs = i_handler.handle_input();
    if (current_inputs)
    {
        current_inputs->execute(*all_objects[0]);
    }
}

void Caller::update()
{
    process_inputs();
    for (int i = 0; i < all_objects.size(); i++)
    {
        all_objects[i]->update();
    }
}

void Caller::draw()
{
    for (int i = 0; i < all_objects.size(); i++)
    {
        all_objects[i]->draw();
    }
}

void Caller::reg_obj(std::shared_ptr<Second_Derived> obj)
{
    all_objects.push_back(obj);
}

int main()
{
    Caller caller{};

    while (true)
    {
        caller.update();
        caller.draw();
    }
}

奖励:如果你能推荐我可靠的(最好是免费的)C++ 多态性资源,因为这是我第二次遇到多态性问题,我很清楚我需要做更多的阅读。

标签: c++polymorphismvirtual-functionscommand-pattern

解决方案


推荐阅读