Confused with object arrays in C++

前端 未结 7 665
后悔当初
后悔当初 2021-01-30 03:56

So I first learned Java and now I\'m trying to switch over to C++. I\'m having a little difficulty getting arrays to work correctly.

Right now I am simply trying to crea

7条回答
  •  [愿得一人]
    2021-01-30 04:14

    The reason is, type of your variable

    players[0]
    

    is Player (object). However, operator "new" (new Player) returns a pointer (Player*)

    If you want to have only one object, correct way to do it will be:

    Player* player = new Player(playerWidth, playerHeight, 20, 1);
    

    And don't forget in C++ you need to clean the mess after yourself - somewhere in the end call

    delete player;
    

    for every object you've created. C++ does not have Garbage Collector - meaning all manually created (by "new") objects stay until you manually delete them.

提交回复
热议问题