copy constructors with overload assignments

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//Game Lobby

#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);
      
      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
             }
You don't need a pointer at all; C++ strings internally manage all that for you.
1
2
string a = "asdf";
string b = a; // does a deep copy 
So there is really no point in using copy constructors then?
In your case, not really, although I am wondering why you are using an intrusive list rather then simply making a Player class and using std::list.
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:
    unsigned int _weight;
};

class Player {
//blah blah
public:
    unsigned int _weight;
    std::vector<Object> inventory;
    //adds up the weights of all Objects in the inventory, plus the player's weight
    unsigned int get_total_weight();
    //etc
};

unsigned int 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.

hoped this helped you


Topic archived. No new replies allowed.