default arguments in constructor

后端 未结 5 1982
失恋的感觉
失恋的感觉 2021-01-15 16:52

Can I use default arguments in a constructor like this maybe

Soldier(int entyID, int hlth = 100, int exp = 10, string nme) : entityID(entyID = globalID++), hea

5条回答
  •  别那么骄傲
    2021-01-15 17:03

    Only trailing arguments can be default arguments. You would need to give nme a default argument or change the order of the arguments that the constructor takes so that hlth and exp come last.

    As regards the assignment you make in the initialiser list what happens there is that the member entityID gets assigned the value that is returned by the assignment of globalID++ to entyID which will be the value of entyID after the assignment. A similar thing happens for name.

提交回复
热议问题