Vector of pointers of objects

Basically I just don't understand how you can access the information of the object through the vector pointers. For example, I need a vector of pointers that points to my object which has a name, wins, loses, and draws. How do I output that information with the pointer pointing at it?

1
2
3
4
struct simple_struct{

    int memb1, memb2, memb3;
};


so... we want a vector of pointers:

vector<simple_struct*> multi({}); //lets say there are 10...

use the dereference operator:

if:
1
2
3
int *i = new int(0);
    (*i == 0) //true
    (i == 0) //false 


so... apply it to vectors:

1
2
3
4
5
multi[5] //returns a simple_struct pointer...

//lets dereference it:
*(multi[5]).memb1
*(multi[5]) //returns the simple_struct at the address multi[5] points to 


1
2
//you can also do it like this:
multi[5]->memb1 // "->" dereferences an object and calls a member in 1 fel swoop. 


Study your pointers.
Last edited on
Okay so I see how that works, the only thing is when I put the information of the pointer its not showing all the information. Here is my code:

if (option == 1)
{
cout << "The current player list is:" << endl;
for (int i = 0; i < AllPlayers.size(); i++)
{
cout << (*(AllPlayers[i])).toString() << endl;
}

}
if (option == 2)
{
cout << "What is the new player's name?" << endl;
cin >> ws;
getline (cin, playerName);

Player newPlayer(playerName);
pointer = &newPlayer;

AllPlayers.push_back(pointer);
cout << "New player as been added." << endl;
}

Option 2 adds the player pointer and Option 1 outputs it as followed:

Name:
Wins: 0
Loses: 0
Draws: 0

and the name doesn't output. I don't know what is wrong because when I output it normally without using the pointer it does output it correctly.
Topic archived. No new replies allowed.