< Previous | Contents | Next >
The operator<<() member function overloads the << operator so I can display a
Lobby object by sending it to cout.
ostream& operator<<(ostream& os, const Lobby& aLobby)
{
Player* pIter = aLobby.m_pHead;
324 Chapter 9 n Advanced Classes and Dynamic Memory: Game Lobby
os << "\nHere’s who’s in the game lobby:\n"; if (pIter == 0)
{
os << "The lobby is empty.\n";
}
else
{
while (pIter != 0)
{
os << pIter->GetName() << endl; pIter = pIter->GetNext();
}
}
return os;
}
If the lobby is empty, the appropriate message is sent to the output stream. Otherwise, the function cycles through all of the players in the list, sending their names to the output stream, using pIter to move through the list.