Skip to main content
gamedev.net
🔒 Locked

Beginning C++ Game Programming

Started by edmondsnake Jun 15, 2009 at 1:48 AM 2 replies 2.1k views
Original Post
edmondsnake
edmondsnake
I saw that there was a post about the Exercise in Chapter 9 for Beginning C++ through Game Programming. But I have the older edition of the book. I get stuck in the following question in the book: Improve the Lobby class from the Game Lobby program by writing a copy constructor and an overloaded assignment operator for it. I have read other references about the copy constructor and the way to overload assignment operator, but I still do not know how to implement it. the lobby is a linkied list of player object, so I feel confused when i think how to make copy constructor and overloaded assignment operator for it. Can anyone help? Thank you very much.

#include <iostream>
#include <string>

using namespace std;

class Player
{
public:
   Player(const string& name = ""): m_Name(name), m_pNext(0) {}
   string GetName() const { return m_Name; }
   Player* GetNext() const { return m_pNext; }
   void SetNext(Player* next) { m_pNext = next; }
private:
   string m_Name;
   Player* m_pNext; //Pointer to next player in list
};

class Lobby
{

   friend ostream& operator<<(ostream& os, const Lobby& aLobby);
   /*friend Lobby& operator=(const Lobby& aLobby);*/
public:
   Lobby(): m_pHead(0) {}
   ~Lobby() { Clear(); }
   void AddPlayer();
   void RemovePlayer();
   void Clear();
 
private:
   Player* m_pHead;
};

void Lobby::AddPlayer()
{
   //create a new player node
   cout << "Please enter the name of the new player: ";
   string name;
   cin >> name;
   Player* pNewPlayer = new Player(name);

   //if list is empty, make head of list this new player
   if (m_pHead == 0)
   {
      m_pHead = pNewPlayer;
   }
   //otherwise find the end of the list and add the player there
   else
   {
      Player* pIter = m_pHead;
      while (pIter->GetNext() != 0)
      {
         pIter = pIter->GetNext();
      }
         pIter->SetNext(pNewPlayer);
   }
}

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;
   }
}


void Lobby::Clear()
{
while (m_pHead != 0)
   {
     RemovePlayer();
     }
}

ostream& operator<<(ostream& os, const Lobby& aLobby)
{
   Player* pIter = aLobby.m_pHead;

   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;
}




int main()
{
   Lobby myLobby;
   int choice;

   do
   {
      cout << myLobby;
      cout << "\nGAME LOBBY\n";
      cout << "0 - Exit the program.\n";
      cout << "1 - Add a player to the lobby.\n";
      cout << "2 - Remove a player from the lobby.\n";
      cout << "3 - Clear the lobby.\n";
      cout << endl << "Enter choice: ";
      cin >> choice;

      switch (choice)
      {
         case 0: cout << "Good-bye.\n"; break;
         case 1: myLobby.AddPlayer(); break;
         case 2: myLobby.RemovePlayer(); break;
         case 3: myLobby.Clear(); break;
         default: cout << "That was not a valid choice.\n";
      }
   }

   while (choice != 0);

   cout << "Press Enter to exit" ;
   cin.ignore(cin.rdbuf()->in_avail() + 1);
   return 0;
}

[Edited by - edmondsnake on June 15, 2009 3:08:19 AM]
Zakwayda
Zakwayda
Use [source][/source] for source code, not quote tags.
Makaan
Makaan
Hello,

A basic implementation of that would be( mostly in pseudocode)
constructor:

Lobby(Lobby & other){@foreach Player P in other.m_pHead     @ make a copy of P in TMP     @ add to your Player list this->m_pHead  the TMP Player with TMP->m_pNextpointing to a element in this->m_pHead list not other.m_pHead@end foreach}

almost the same with the operator = :

Lobby& operator = (Lobby & other)
{
@destroy your list this->m_pHead;
same code from here on
return (*this);
}


Raxvan
DekuTree64
DekuTree64
My answer: Adding copy/assignment would not be an improvement to that class :p

The player structures seem to be owned by the lobby, so the copy lobby would have to make its own list of players, so the player class would need a copy constructor too. But having 2 copies of a player lobby floating around seems like a bad idea to begin with. Doesn't seem like a very good book if they're trying to teach you to write copy constructors for every class you create, even when you never plan to use them. Writing code that will never be used is dumb.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.