The above object creates a CPlayer object on the stack, so it does not need new . You need to use new if you are trying to allocate a CPlayer object on the heap. If you use heap distribution, the code will look like this:
CPlayer *newPlayer = new CPlayer(position, attacker);
Please note that in this case we use a pointer to the CPlayer object, which will need to be cleared using the appropriate call before delete . An object allocated on the stack will be automatically destroyed when it goes out of scope.
In fact, it would be easier and clearer to write:
CPlayer newPlayer(position, attacker);
Many compilers optimize the version you posted above and read more clearly.
Timo Geusch Nov 19 '09 at 17:00 2009-11-19 17:00
source share