C++ Object without new

前端 未结 4 1081
礼貌的吻别
礼貌的吻别 2020-12-07 08:31

this is a really simple question but I havn\'t done c++ properly for years and so I\'m a little baffled by this. Also, it\'s not the easiest thing (for me at least) to look

4条回答
  •  温柔的废话
    2020-12-07 09:13

    This expression:

    CPlayer(position, attacker)
    

    creates a temporary object of type CPlayer using the above constructor, then:

    CPlayer newPlayer =...;
    

    The mentioned temporary object gets copied using the copy constructor to newPlayer. A better way is to write the following to avoid temporaries:

    CPlayer newPlayer(position, attacker);
    

提交回复
热议问题