[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!
 
More importantly, why do you need to do this?

You could loop over all your initialised Player objects and call playerx.RandomVariable = 5 which will at least give you control over, if for example, player4 needed to have a different RandomVariable figure. Making a global variable could result in a world of pain down the line.
 
I believe this can be achieved by using static variables.

For example:

Code:
class Player
{
   private:
             static int RandomVariable;
		
    public:     
             Player();
             static void setRandom(int randomVar); //note it is static 
};

Code:
int Player::RandomVariable = 0; //Must be declared outside the header

void Player::setRandom(int randomVar)
{
	RandomVariable = randomVar; //Set the variable
}

int main(){
    Player myPlayer();
    Player::setRandom(100); //Will change the variable
                            //for all instances of the class
}

Hope this is what you were after.
 
More importantly, why do you need to do this?

You could loop over all your initialised Player objects and call playerx.RandomVariable = 5 which will at least give you control over, if for example, player4 needed to have a different RandomVariable figure. Making a global variable could result in a world of pain down the line.

this isn't what the OP is asking for

you should be ashamed of yourself
 
harsh, yes - but you're suggesting something that's extremely poor programming style and doesn't answer the question that the OP is asking

i say this from 13 years of solid industry experience in writing software - for your reference
 
@OP we need to know what you're trying to achieve, what does the random variable actually mean?

e.g. if it's a TeamId you probably want to create a Team object with a collection of players.
 
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?
 
*confused*

It sounds like you want your member to be able to tell you two values..

In the scenario you described above, i guess you might just want two members, one static and one not. The static member would be the same accross all instances of the Player class, where as each Player would have their own health too. Though I can't see why you'd want to change the origional HP?

Code:
#define STARTING_HP 100

class Player
{
public:

	Player(void);

	static void SetOriginalHP(int hp){ s_iOriginalHP = hp; }
	void	SetHP(int hp)		{ m_iCurrentHP = hp; }
	int	GetHP(void)		{ return m_iCurrentHP; }

	void	TakeDamage(int damage)	{ m_iCurrentHP -= damage; }

private:	

	int		m_iCurrentHP;
	static int	s_iOriginalHP;

};

Code:
int Player::s_iOriginalHP = STARTING_HP;

Player::Player(void) :
m_iCurrentHP(STARTING_HP)
{
}

int main(void)
{
	Player somePlayer();
	Player.TakeDamage(10);	// knocks 10 HP off, now somePlayer has 90HP

	Player::SetOriginalHP(70);	// Do this to change s_iOriginalHP across all instances :S
}
 
Back
Top Bottom