I have a question regarding classes and how their members can be updated.
So basically, I have a simple class
class Player
{
public:
Player();
std::vector <std::string> hand = {"r4", "r1", "g5"};
};
Player::Player()
{
}
and I added instances of these classes to another vector
std::vector <Player> players;
Player p1;
Player p2;
players.push_back(p1);
players.push_back(p2);
But then I try to update the vectors of the initialized classes inside the vector storing the classes
//doesn't work
//p1.hand.push_back("test1");
//p2.hand.push_back("test2");
//works
players[0].hand.push_back("test1");
players[1].hand.push_back("test2");
for (int i = 0; i < 2; i++)
std::cout << players[i].hand[(players[i].hand.size() - 1)] << std::endl;
I am confused why it is not adding the test strings to the vectors of the classes in the players array with the first method. Is it because it's not the same instance of the class as when I first initialized it? If someone could clarify this for me that would much appreciated. Thanks!