Hello
I have a class called Player and have two function SetGold() and GetGold(). I also have a class called HUD and I setGold() to 10 in HUD class. now I want to GetGold() in tower class but when i cout<<GetGold() in tower class I get 0 not 10 ! why is that? and how can i fix it?
BTW Happy new year :D
Player.h
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
class Player
{
public:
Player();
~Player();
void SetGold(int Gold);
int GetGold();
private:
short Gold;
};
HUD and Tower are both holding copies of the original. Their values don't have to be the same at all times. Make one of them a pointer or reference to the other, so that there is only one physical variable in memory, representing Gold.
thank you. I'm sorry I'm a newbie and in the book i'm reading I still didn't cover pointers or references. Can you please give me an example on how can I use pointers or references ?
I don't think that references or points are really required. I think the problem is in the SetGold() function. Why don't you just replace that function with this:
its the same. notice that i use the this-> pointer to indicate that I'm using the Gold variable in the class not in the function.
anyways I changed the variable name and its the same.
If you guys can give me a code example on how can i use pointer or reference I would be very thankful.
I forgot Happy new year :D
I tried this but its also not working.
in my understanding this will return the memory address of Gold variable.
if its returning the memory address then why its still returning 0?
1 2 3 4 5
int Player::GetGold()
{
short *rGold = &Gold;
return *rGold;
}
You are setting the gold to 10 in the HUD class and then trying to extract it in the TOWER class. However the instances of player in both files are completely seperate.
Suppose we made an int i in one cpp file, and then made an int i in another cpp file without any connection between the two. i would be a completely seperate variable and so you cannot access the data from one with the other. Same thing here. You are creating an object called Player from the class Player in the Tower Class and then you are making another object that happens to be called Player from the class Player in the HUD Class. It isn't the same instance and so they cannot communicate. This is especially true since they are both private members of other classes. Note that the other classes (HUD and TOWER) don't include each others .h files and so they don't even know that the other ones exist.
You'll have to use Player.SetGold(10) in the constructor of Tower to use it in Tower. Not the constructor of HUD.
Also, to keep your classes and objects of classes organized, I suggest not naming objects with the same name as their type. This makes things VERY confusing!
@Stewbond
ok thank you. the thing is I need to use Player.SetGold(10) in the HUD class because there are some calculations happening there. However I also want to GetGold() in the Tower class.
So how can I use use Player.SetGold(10) in the HUD class and still get gold in Tower class?
Here is an idea that may help you to communicate. It does use the references and pointers as suggested earlier:
1 2 3 4 5 6 7 8 9
int main()
{
Player Me(); //Creates an instance of Player that we will watch
HUD TheHUD(&Me); //TheHUD will be associated with Me. We pass the address in, not the object.
Tower TheBigScaryTower(&Me); //TheBigScaryTower will also be associated with me. We pass the address in.
// Now both TheHUD and TheBigScaryTower can access/set the public members of the same player.
return 0;
}
Now we need to adjust the HUD and Tower constructors so that they will accept the reference:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//HUD header
class HUD
{
public:
HUD(Player* InputPlayer);
~HUD();
private:
Player* HUDPlayer; //make a Player pointer, not a player itself.
};
// ctor
HUD::HUD(Player* InputPlayer) // Here we get the address of the object we'll track, not a copy of the data.
{ // This lets us set values of the original object.
HUDPlayer= InputPlayer; //Lets save the address to a private pointer.
HUDPlayer->SetGold(10); // Now we can use that object as if it was in our own class.
}
Now we have our big scary tower and our HUD working with the same player.
Always remember that in Object-Oriented languages like c++, the classes can be reused to create many objects (players, towers or HUDs in this case). By naming the objects with identical names to their classes and each other we will have a very hard time figuring out which information is saved where. Get in the habit of differentiating names and you'll find that the classes and pointers become much easier to handle.
wow thank you very much its working. that looks so very complicated. I'll keep reading the code till I understand it and of course read about pointers and references. the idea of pointers and references is simple but applying it is a bit hard.