改进了13参数构造函数

Cas*_*sey 11 c++ parameters constructor

感谢Microsoft为Atomineer Utils的Intellisense和Atomineer ...所有这些参数都是必需的且不可变的.

有一个更好的方法吗?

/**************************************************************************************************
 * <summary>Initializes a new instance of the ADTBattleCharacter class.</summary>
 * <param name="name">         The name of the character.</param>
 * <param name="max_HP">       The maximum hit points.</param>
 * <param name="max_MP">       The maximum magic power.</param>
 * <param name="strength">     The strength.</param>
 * <param name="agility">      The agility.</param>
 * <param name="attack_power"> The attack power.</param>
 * <param name="defense_power">The defense power.</param>
 * <param name="gold">         The gold carried by the character.</param>
 * <param name="experience">   The experience the character is worth.</param>
 * <param name="stop_resist">  The character's resistance to stopspell.</param>
 * <param name="sleep_resist"> The character's resistance to sleep.</param>
 * <param name="hurt_resist">  The character's resistance to hurt/hurtmore.</param>
 * <param name="spell_list">   Available spells.</param>
 **************************************************************************************************/
ADTBattleCharacter(std::string name, unsigned char max_HP, unsigned char max_MP,
                   unsigned char strength, unsigned char agility,
                   unsigned char attack_power, unsigned char defense_power,
                   unsigned short gold, unsigned short experience,
                   double stop_resist, double sleep_resist, double hurt_resist,
                   std::bitset<SPELL_MAX> spell_list);
Run Code Online (Sandbox Code Playgroud)

Nic*_*las 33

看看你的具体案例,在我看来你没有把事情搞得很好.

从概念上讲,系统中的角色具有:

  • 一个名字.
  • 一个stat块,包含他们的基本统计数据(HP,防御等).
  • 角色的次要属性(经验).
  • 一个库存,包括他们当前的法术和他们的黄金列表,以及其他潜在的东西.

这是4个参数,而不是13.当你编写一个函数,并且你发现它正在采用大量参数时,其中一些参数在概念上相互关联的可能性很大.并且其他功能想要使用这些链接参数的几率也很高.

例如,您可能想要显示角色的stat块.这样做的功能真的需要角色吗?没有; 它只需要stat块.所以应该采用stat块对象.

就像角色的构造函数一样.

  • +1正如Alan J Perlis所说:"如果你有一个包含十个参数的程序,你可能会错过一些." (4认同)
  • @Casey:那么?分布在4个类中的13个参数是每个构造函数平均3.25个参数.有4个参数的构造函数有问题吗?您是否考虑使用4D矢量点积函数来获取8个参数?没有; 它需要2,恰好每个都有4个值.同样,它不仅仅是关于*this*的功能; 这是关于正确的数据组织.无论构造函数采用多少参数,4D向量都是*一个对象*.正如stat块是单个对象一样,无论其中有多少个统计数据. (2认同)