1

So, I'm trying to store objects that are all derived from the same base class in a Map, and then call a member function from a specific object in the Map. The problem is, that it calls the function in the base class instead of the one from the derived class.

Here is the base class:

 #pragma once
 class State
 {
public:
   virtual ~State(){};

   virtual void update(double dt){}
   virtual void render(){}
};

The derived class:

    #pragma once
    #include "State.h"

    class State_Splash : public State
    {
    public:
        State_Splash();
        ~State_Splash();

        void update(double dt);
        void render();
    };

The class with the Map that stores the state objects:

Header file:

    #include "State.h"

    #include <map>
    #include <string>

    class StateManager
    {
    public:
        StateManager();
        ~StateManager();

        std::map<std::string, State> m_StateMap;

    public:
        void AddState(std::string stateId, State state);
    };

Implementation file:

    #include "StateManager.h"

    StateManager::StateManager(){}

    StateManager::~StateManager(){}

    void StateManager::AddState(std::string stateId, State state){

        m_StateMap.insert(std::pair<std::string, State>(stateId, state));
    }

And last in main, I'm using a StateManager object, and referencing the map in it.(I cut it down a bit and only included the necessary parts)

 int main(int argc, char **argv){

     StateManager stateManager;
     State_Splash _splash;

     stateManager.AddState("splash", _splash);


     stateManager.m_StateMap["splash"].render();//just calls the base class function
Dortimer
  • 619
  • 8
  • 25

0 Answers0