Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualshaqb4

Posted 16 December 2012 - 05:07 PM

Hi, I'm reading the c++ book Beginning C++ Game Programming by Michael Dawson and one of the exercises is to make a copy constructor for a game Lobby object which has a linked list of Player objects on the heap. It's supposed to create a new memory address for all the copy's data members so that the list pointers don't point to the same instances as the original.

I created a constructor that seems to work, but I think it might more complicated than needed.

Here's the Lobby class:

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

Here's the Player class:

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

and here's the copy constructor:

Lobby::Lobby(const Lobby& l)
{
	if (l.m_pHead != 0)
	{
		m_pHead = new Player;
		*m_pHead = *l.m_pHead;
		Player* pIter = m_pHead->GetNext();
		Player* pHeap;
		while (pIter != 0)
		{
			pHeap = new Player;
			*pHeap = *pIter;
			pIter = pIter->GetNext();
		}
	}
	else
	{
		m_pHead = 0;
	}
}

Thanks, any help/explanations would be appreciated.

#1shaqb4

Posted 16 December 2012 - 05:06 PM

Hi, I'm reading the c++ book Beginning C++ Game Programming by Michael Dawson and one of the exercises is to make a copy constructor for a game Lobby object which has a linked list of Player objects on the heap. It's supposed to create a new memory address for all the copy's data members so that the list pointers don't point to the same instances as the original.

I created a constructor that seems to work, but I think it might more complicated than needed.

Here's the Lobby class:

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

Here's the Player class:

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

and here's the copy constructor:

Lobby::Lobby(const Lobby& l)
{
	if (l.m_pHead != 0)
	{
		m_pHead = new Player;
		*m_pHead = *l.m_pHead;
		Player* pIter = m_pHead->GetNext();
		Player* pHeap;
		while (pIter != 0)
		{
			pHeap = new Player;
			*pHeap = *pIter;
			pIter = pIter->GetNext();
		}
	}
	else
	{
		m_pHead = 0;
	}
}

Thanks, any help/explanations would be appreciated.

PARTNERS