So Im reading a chapter in Beginning C++ Game Development and got stuck on a chapter. The chapter goes along with talking about classes and using copy constructors and overload assignments which I get the idea of them. One of the exercises in the book said modify the program and add a copy constructor and a overload assignment operator for it and this just doesnt seem to work when I try to do it. Here is the code
//Game Lobby
#include <iostream>
#include <string>
usingnamespace 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);
public:
Lobby(): m_pHead(0), m_pTail(0) {}
~Lobby() {Clear();} //destructor
Lobby(const Lobby&);
void AddPlayer();
void RemovePlayer();
void Clear();
void Copy();
private:
Player* m_pHead;
Player* m_pTail;
};
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;
m_pTail = pNewPlayer;
}
//otherwise find the end of the list and add the player there
else
{
m_pTail->SetNext(pNewPlayer);
m_pTail = m_pTail->GetNext();
}
}
void Lobby::RemovePlayer()
{
if (m_pHead == 0)
{
cout << "The game lobby is empty. No on 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);
return 0;
}
I try to use a copy constructor which uses dynamic memory so its not a shallow copy of the variables, but I cant seem to use the GetName() function from player to make a copy of the name.
If you are using strings, copying by value will automatically do a deep copy. And I don't see your copy constructor or overloaded assignment operator, so I can't really say anything about any troubles you might be having with those.
Oh sorry must have not had it in when I saved. I have no idea what to do with the overload assignment operator would it be the = operator. I mean I just am really confused with it thought I understood the concept but I just do not know how to apply it. m_pCurrent is declared as a string variable and then I need to get the name from the addPlayer function but I don't know how to do that.
1 2 3 4 5
Lobby(const Lobby&)
{
m_pCurrent = new string;
*m_pCurrent = ;//Here is what I don't know what to put
}
Its just an exercise in this book that I am reading, they are trying to show how to use an object in an object you know. I thought the same thing but I sort of want to know how this works just to get an idea.
Ah, ok then. IMO it would be a better idea to do something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class Object {
//blah blah
public:
unsignedint _weight;
};
class Player {
//blah blah
public:
unsignedint _weight;
std::vector<Object> inventory;
//adds up the weights of all Objects in the inventory, plus the player's weight
unsignedint get_total_weight();
//etc
};
unsignedint Player::get_total_weight() {
//function stuff
}
Sorry firedraco but I don't get what that code is supposed to do and sorry I suck at trying to explain things. Its sort of confusing the exercise tells me to make a copy constructor in the Lobby class but I don't know what to copy, would it not make more sense to be making a copy in the player class? Ugh I might just skip over this part since the more I think about it the more confused I get about it.
a copy constructor is a constructor that creates and object, and rather than storing default values, you will be able to pass in an objrct and copy its values to the new object. You should be able to create a copy constructor for any class.
the constructor would look something like this
Class(const Class& aClass)
{
private_var1 = aClass.private_var1;
private_var2 = aClass.private_var2;
private_var3 = aClass.private_var3;
.
.
.
// continue to set the new private values = to aClass private variables
}
you would use this in main like the following.
Class x,
x.set......
x.set......
x.set......
Class y(x);
now x is a different object , but there values are the same.