任务是也许不是非常复杂的,只要你总是可以忽略的默认值(例如为Nothing)指定参数。我们可以省略的参数应该是最后一个,或者前面的省略。所以,你所描述的是在相对比较容易千篇一律:
class Stats
{
public:
// some stat methods and data
};
enum Item
{
Nothing,
Something1,
Something2,
Something3
};
class A
{
explicit A(Item item = Nothing);
A(const Stats& stats, Item item = Nothing);
};
A::A(Item item)
{
// in both constructors just recognize if the item is Nothing
if (item == Nothing)
{
// default, no item
}
else
{
// deal with an item
}
}
所以,你的客户端代码将能够做到:
// we imply item != Nothing here
A one; // default
A two(item); // with an item
A three(stats); // with stats
A four(stats, item); // with stats and an item
当然,你应该以某种方式指定项目和统计。
你能发布你的相关代码吗? – Chrismas007 2014-12-07 05:32:39
不知道为什么你甚至想要这样做,而不是仅仅让一个成员指出敌人是否有物品。 – 2014-12-07 08:31:36