C++ Object without new

前端 未结 4 1088
礼貌的吻别
礼貌的吻别 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:01

    newPlayer is no dynamically allocated variable but an auto, stack-allocated variable:

    CPlayer* newPlayer = new CPlayer(pos, attacker);
    

    is different from

    CPlayer newPlayer = CPlayer(pos, attacker);
    

    newPlayer is allocated on the stack via the normal CPlayer(position, attacker) constructor invocation, though somewhat verbose than the usual

    CPlayer newPlayer(pos, attacker);
    

    It's basically the same as saying:

    int i = int(3);
    

提交回复
热议问题