< Previous | Contents | Next >
The Lobby:: RemovePlayer() member function removes the player at the head of the line.
void Lobby::RemovePlayer()
{
if (m_pHead == 0)
{
cout << "The game lobby is empty. No one to remove!\n";
}
else
{
Player* pTemp = m_pHead; m_pHead = m_pHead->GetNext(); delete pTemp;
}
}
The function tests m_pHead. If it’s 0, then the lobby is empty and the function displays a message that says so. Otherwise, the first player object in the list is removed. The function accomplishes this by creating a pointer, pTemp, and pointing it to the first Player object in the list. Then the function sets m_pHead to the next thing in the list—either the next Player object or 0. Finally, the function destroys the Player object pointed to by pTemp. Check out Figure 9.15 for a visual representation of how this works.