Confused with object arrays in C++

前端 未结 7 645
后悔当初
后悔当初 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:19

    In Java, when you use the keyword "new" you actually get back a pointer to an object. This is the only way to instantiate an object type in Java. So when you say you have an "Array of objects" in Java, it's more correct to say that you have an array of pointers to objects.

    C++ does not hide the fact that objects are pointers. You can have a variable referencing an object, or you can have a variable referencing a pointer to an object.

    In your example, you need to explicitly declare it an as an array of pointers to objects.

    Players **players = new (Player*)[1];                         // Create an array of player pointers
    players[0] = new Player(playerWidth, playerHeight, 20, 1);    // Create a single player
    

    And while C++ allows you to explicitly create objects using the keyword new, you must be sure to cleanup your objects once you are done, otherwise they will never be deallocated (known as a memory leak).

    This is one of the major differences between C++ and Java; Java's objects are garbage collected and the programmer doesn't have to worry about managing the lifetime of an object.

    Once you are done, you will need to cleanup both the individual player that you allocated, as well as the array. Good rule of thumb is that every call to new should correspond to a call to delete.

    delete players[0];  // delete the player pointed to by players[0]
    delete[] players;   // syntax for deleting arrays
    

    However, something interesting to note is that unlike Java, where objects are allocated on the heap, you can create objects on the stack in C++ as if they were primitive types (like int, float, char). This allows you to have objects that are locally scoped, as well as contiguously aligned in memory. There is no way to do this in Java.

    If you allocate an array of objects this way, then the default constructor is called for each object in the array.

    Player p;                           // This calls the default constructor and returns a Player object
    
    Players *players = new Player[5];   // Create an array of player objects
    players[0].playerWidth = 8;         // valid because the object has already been constructed
    
    delete[] players; // don't forget to cleanup the array.
                      // no need to cleanup individual player objects, as they are locally scoped.
    

    EDIT: As some others have mentioned, using a std::vector instead of an array is probably easier in your case (no need to worry about memory allocation) and is on the same order of performance as an array; however I think it is extremely important to become comfortable with the notion of pointers in C++ as they help you understand how memory is organized.

    Here is the syntax for creating a vector of Player pointers.

    std::vector<Player*> players(1); // Creates a vector of pointer to player with length 1
    players[0] = new Player(playerWidth, playerHeight, 20, 1); // Create a new player object
    delete players[0];                                         // delete the player
    

    And the syntax for creating a vector of actual Player object instances (this one is the most preferred solution):

    std::vector<Player> players(5); // Creates a vector of five player objects
    players[0].playerWidth = 8; //already constructed, so we can edit immediately
    //no cleanup required for the vector _or_ the players.
    
    0 讨论(0)
提交回复
热议问题