change a value in an array.

okay so i am using composition and i have

1
2
3
4
5
6
7
8
//loc is to get the location of the account I want 
 accounts[loc].getbalance()
//I would like to add 10 to this value and the initial value is 5
//I try

accounts[loc].getbalance() = accounts[loc].getbalance() + 10;
//this doesnt allow it to work
//how can i change the value of accounts[loc].getbalance()? 
Okay, so what is function getbalance()?

As I hinted above, it's function - and function has its return value. In this example, it looks like it has a value of int.

If you understand some of C++, then you might want to know that this function probably looks like this:
1
2
3
4
5
//general definition; not including class
int getbalance()
{
 return balance;
}


This way, this function only RETURNS something. You can't access balance variable, you can only see it(know what value it holds). To access it, you would either have to do:

 
accounts[loc].balance = accounts[loc].getbalance() + 10;


But mostly, if you have getter function like getbalance(), you won't be able to access balance directly. Therefore, you may hope(or just check) that this class has a setter, which you could use (probably)like this:
 
accounts[loc].setbalance( accounts[loc].getbalance() + 10 );
Yes you are right that is the function int getbalance that I have. So I tried
accounts[loc].setbalance( accounts[loc].getbalance() + 10 );

and it worked like a charm . Thanks a lot man ! I can't believe I didn't notice my mistake of int getbalance. Thanks a lot !
Topic archived. No new replies allowed.