引用正在调用基类方法

Reference is calling base class methods?

本文关键字:基类 类方法 调用 引用      更新时间:2023-10-16

子引用调用其父方法而不是自己的方法的原因是什么?我让父对象和子对象都实现虚拟方法。

问题-->在此代码示例中,initial.setup正在调用State的安装程序<--问题

void StateMachine::initialState(State& initial)
{   
            initial.setup();
            currentStates.add(&initial);
}
void StateMachine::setup(Actor& actor, HashMap<State>& allStates)
        {
            _actor = &actor;
            _allStates = &allStates;
        }

这是我的控制器,它给状态机一个初始状态:

HashMap<State> _states; //This is defined in Controller
void PlayerController::setup(Actor& actor)
{   
        //Factory* actorFactory = new Factory();
        //actorFactory->setup();
        _actor = &actor;
        _stateMachine.setup(actor, _states);

        DrinkState drinkState(actor, _stateMachine);
        MineState mineState(actor, _stateMachine);
        SingState singState(actor, _stateMachine);
        BrawlState brawlState(actor, _stateMachine);
        SleepState sleepState(actor, _stateMachine);
        IdleState idleState(actor, _stateMachine);
        CombatState combatState(actor, _stateMachine);
        _states.add(idleState.to_string(), idleState);
        _states.add(mineState.to_string(), mineState);
        _states.add(drinkState.to_string(), drinkState);
        _states.add(singState.to_string(), singState);
        _states.add(brawlState.to_string(), brawlState);
        _states.add(sleepState.to_string(), sleepState);
        _states.add(combatState.to_string(), combatState);
        //Put actor in idle state
        _stateMachine.initialState(_states.getValue(idleState.to_string())); //_states.getValue returns a reference
} 

国家间关系。

class State
    {   
        protected:
            Actor* _actor;
            IStateCallback* _statemachine;
        public:
            State();
            State(Actor& actor);
            State(Actor& actor, IStateCallback& stateMachine);
            virtual void setup();
            virtual void breakdown();
            virtual void tick(float dTime);
            virtual void registerTransitionCallback(IStateCallback& statemachine);
            virtual void switchState(string nextState);
            virtual string to_string();
            virtual bool operator==(State& s); 
            virtual ~State();
    }; 
class MineState : public State
    {   
        private:
            int successChance;
            int maxGold;
        public:
            ~MineState();
            MineState(Actor& actor);
            MineState(Actor& actor, IStateCallback& statemachine);
            virtual void setup();
            void breakdown();
            virtual void tick(float dTime);
            void switchState(string nextState);
            string to_string();
    };

Hashmap的getValue函数:

template<class T>
    T& HashMap<T>::getValue(string key)
    {   
        int bucket = hashString(key);
        HashNode<T>* temporary = hashMap[bucket];
        while (temporary != NULL)
        {   
            if (key.compare(temporary->getKey()) == 0)
            {   
                return temporary->getValue();
            }   
            temporary = &(temporary->getNextNode());
        }   
        T *ret = NULL;
        return *ret;
        //return (T) NULL;
    } 

据我所见,问题是HashMap<State>包含States。当您调用:

_states.add(idleState.to_string(), idleState);

这是调用State复制构造函数,并在哈希表中存储一个基State——关于派生类的所有信息都已丢失。你是切片的受害者。

由于不能将引用存储在容器中,因此需要执行其他操作。这有点难看,但如果您将哈希表更改为:

HashMap<State*> _states;

然后插入State指针,您将获得您期望的多态行为。(当然,可以使用像std::unique_ptr这样的智能指针来代替原始指针。)