jeudi 21 mars 2019

How is memory allocated when the array members' size is not known?

Imagine the following scenario:

class infoPlayer
{
private:
//The details are not important, just notice that the size of everything can change at any time
    const std::string name;
    std::shared_ptr<std::string> hand; //share ownership with player
    std::vector<std::string> cantHave; //I know he does not have
    std::vector<std::string> iveShown; //I have shown to him
    std::vector<std::vector<std::string>> possiblyHas; //Has to have one
public:
    infoPlayer(player);
    ~infoPlayer();
};

But then I have another class:

class player
{
private:
//Here I want an array of infoPlayer
};

I want my playerto contain the info of every other player. I have tried std::vector<infoPlayer>, std::unique_ptr<infoPlayer>, but it doesn't work.

The only thing I did, which worked was infoPlayer* notebook. Imagine that then, in the player's constructor I initialize notebook (new infoPlayer[x]). How is memory allocated for that number of infoPlayers. The size of each infoPlayer will increment throughout the game. Will this wokr?

How can I create a list of infoPlayers as a member of my player?

Aucun commentaire:

Enregistrer un commentaire