引用正在调用基类方法
Reference is calling base class methods?
子引用调用其父方法而不是自己的方法的原因是什么?我让父对象和子对象都实现虚拟方法。
问题-->在此代码示例中,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>
包含State
s。当您调用:
_states.add(idleState.to_string(), idleState);
这是调用State
复制构造函数,并在哈希表中存储一个基State
——关于派生类的所有信息都已丢失。你是切片的受害者。
由于不能将引用存储在容器中,因此需要执行其他操作。这有点难看,但如果您将哈希表更改为:
HashMap<State*> _states;
然后插入State
指针,您将获得您期望的多态行为。(当然,可以使用像std::unique_ptr
这样的智能指针来代替原始指针。)
相关文章:
- 是否可以使用基类非虚拟方法中的派生类虚拟方法?
- 通过基类接受方法转发派生 UniquePtr 的右值会移动引用而不是复制
- C++初始化之前派生类调用基类的方法
- 为什么派生类的实例从基类调用方法?
- 为什么允许将派生类的方法static_cast为基类的方法?
- 从基类调用继承类的方法.C++
- 我无法重写基类的方法,因为我的派生类是模板化的
- C++继承,如何在基类的方法中调用子类的方法?
- C ++中有没有办法让派生类重写基类静态方法
- 如何将模板派生类的方法提取到非模板基类中
- 将指针从派生到基类的方法的最佳方法
- 当类不需要重写从多个基继承的方法时该怎么办
- 抽象基类的派生类未正确覆盖基纯虚拟方法
- C++抽象基模板类非空方法
- CRTP:基于派生类内容的基类启用方法
- 通过"a pointer of the base class"访问未在基类中声明的子类的方法或属性(动态)
- 当可变参数模板类继承自模板参数时,在调用基类型的方法时扩展参数包
- SWIG:在派生类中处理基类重载方法
- 在派生类的方法中使用基类的方法
- 仅通过基类"create"方法将对象创建为共享指针