我正在编写一个简单的游戏来学习获得更多的 C++ 经验,并且我知道我觉得多态性几乎可以工作,但没有。在这个游戏中,Party
通过 a 的移动相当线性Map
,但偶尔会Fork
在路上遇到 a。叉子(基本上)是一个std::vector<location*>
.Original 我打算在Party
成员函数中编写如下代码:
if(!CurrLocation->fork_.empty())
// Loop through forks and show options to the player, go where s/he wants
else
(CurrLocation++)
但我想知道以下的一些变体是否会更好:
CurrLocation = CurrLocation->getNext();
Fork 实际上是从 Location 派生的,并重载了一些新功能getNext()
。但是在后一种情况下,location
(一个低级结构)必须是向用户呈现消息而不是“传递这个备份”的那个,我不觉得它是优雅的,因为它location
与UserInterface::*
.
你的意见?