changing private reference through external function?

Oct 2, 2014 at 11:32pm
Hey guys, quick question.
How would I be able to change a private variable from one class in an external function, something like a reference. Here is my code and it doesn't work but hopefully you can see what I'm trying to do.

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
//Player class
class Player
{
private:
   int dollars;
public:
   int getDollars();
};
Player::Player()
{
dollars = 50;
}
int Player::getDollars()
{
return dollars;
}


//External function
buy(int &dollar) //something is wrong here, I want to pass dollars as a ref but how?
{
 //bla bla bla you buy stuff
 //Everytime you buy something I want the value of dollars changed back in 
 //the player class to simulate the player spending his hard earned dollars
std::cout << dollar << std::endl; //this should print out current value of dollars in player class
}

//in main()
main()
{
Player Man;
buy(Man.getDollars());
}


I understand encapsulation and data hiding are in play here, I would like to know an effective way to change private data through an external reference function however.Is it possible? is it bad practice?
Last edited on Oct 2, 2014 at 11:35pm
Oct 3, 2014 at 12:37am
Yes it is possible - you need to return the variable by reference as well. Like this:
1
2
3
4
5
6
7
8
class Player {
    public:
        int& getDollars()       { return dollars; }
        int  getDollars() const { return dollars; }

    private:
        int dollars;
};


You notice I provide two versions of the function - one for normal, and one for when you have a const version of your object. I could have returned a const reference, but I may as well just copy it considering its only an int.

As for if its bad practice: generally. Usually, the only thing that some outside of a class should know is its interface, what goes on underneath could change massively and it wouldn't care. In the case that you return a member variable, now everything knows that your Player class has a 'dollars' member, might base some code around that fact, and if you decide to change it then you have a slight problem on your hands.

If you find that the variable itself IS an important part of the interface, though, you could either have a reference obtaining function like I demonstrated above or you could just make the variable public, actually putting it into your interface.
Oct 3, 2014 at 2:07am
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
#include <iostream>

class Player
{
private:
   int dollars;
public:
	Player(); //
	int getDollars();
	void reduceDollars(double percent) {dollars = dollars*percent/100; }; //
};

Player::Player() {
   dollars = 50;
}

int Player::getDollars() {
   return dollars;
}

void buy(Player param) { //
	param.reduceDollars(80); //
	std::cout << "reduced to 80 %" << std::endl;
	std::cout << "now, $ : " << param.getDollars();
	return;
}

main()
{
Player Man;
buy( Man); //
}
Oct 3, 2014 at 6:36am
Thanks guys, I realize this question has answers much simpler than I anticipated.
Sometimes I feel that it is wrong to have functions that do exactly what I want them to do because I want to have small, well written code.

As for the classes thing, it does defeat the purpose of having a variable private if other functions and classes are going to have direct access over it.
Topic archived. No new replies allowed.