[C++] Changing Class Variables from outside of the class

Associate
Joined
18 Nov 2008
Posts
2,430
Location
Liverpool
Heres the scenario, I have a class:

Code:
class Player
{
    public:
        Player();
        int RandomVariable;
};
Player::Player()
{
    RandomVariable = 10;
}

And I want to change that class variable (RandomVariable) doing something like this:
Code:
int main()
{
    Player player;
    player.RandomVariable = 5;
}

But this only changes the variable of Player that has been created in main under the instance of player, how do I make it actually change the class variable so that it then changes it over the entire program, rather than only in that instance.

Sorry for poor wording, only just started C++ so finding it difficult to express what I mean!
 
The scenario is that the class represents the player, and the player has hitpoints (Health).

Say he starts on 100, gets into a fight and goes down to 70, I want the original hitpoints variable of the class Player to be altered, not the hitpoints variable in the instance of the class player
 
I believe this can be achieved by using static variables.

For example:

<Snip>

Hope this is what you were after.

Thank you very much this was indeed what I needed, is the reason it works because the variable is made private or because it is made static?
 
Back
Top Bottom