Clarification on how member functions work when objects are passed as arguments?

Hello,

I'm writing a little program to help myself get used to classes in c++.
It's something like an accounting simulation.
I think that I have a problem with understanding how to call member functions when objects are being passed as arguments. Following is my code involving the three important member functions:

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
void Account::transfer(double amount, Account payee)
{ 	payee.deposit(withdraw(amount));
}

double Account::withdraw(double amount)
{	if (amount > acct_bal)
		{	std::cerr << "Cannot withdraw an amount greater than " << acct_bal << "." << std::endl;
			return 0;
		}

	if (amount > 0) 
		{	acct_bal -= amount;
			return amount;
		}
	
	if (amount < 0)
		{	std::cerr << "Illegal or negative amount. Aborting. "<< std::endl;
			return 0;
		}
}

void Account::deposit(double amount)
{	if (amount > 0)
		acct_bal += amount;
	else
		std::cerr << "Illegal deposit amount. Aborting.";
}


So, transfer receives a arguments for the amount, then payee (which is another Account type object.) I suspect that the call payee.withdraw(etc.) is somehow incorrect. A similar call directly to the object works, ie:
 
 an_account.deposit(other_account.withdraw($$$))


The problem with the member function is that the withdrawal effect takes place, but the deposit doesn't register with the "payee" object somehow. There are no errors, either cerrs I've coded or runtime errors. I've messed around with the keyword "this" but haven't had any luck yet. Should I be passing a pointer instead of an object?

If more extensive code is desired, or I haven't been clear, feel free to ask questions.
Last edited on
When you pass a thing to a function, the function gets a copy of the thing. This is true for all data types.
Passing payee by pointer would solve this problem. Passing by reference would be the same, but with more comfortable syntax.
Well if I get it right you want to be able to change the data members of your class within your member functions, correct?

Then the most natural way is just add and & to make calls by reference in your functions. Something like:
void Account::transfer(double amount, Account& payee)
In this case amount is called by value and payee by reference meaning payee can be changed within the function. In a different case you could still use a reference (to avoid useless copies od object for example) but with const (e.g. const Account& payee) which prevents accidental payee changes.

There is no need in this case to use calls by pointer since its more unnatural (use of * or -> instead of just the variable names)
Thank you both so much for the clarification. So a copy of the "payee" data was getting created and modified instead of the actual object. Just out of curiosity, what happened to that copy? Destroyed as out of scope when the function terminated? I suppose those two behaviors are exactly what I'd expect with a built in type, so I shouldn't be surprised.

I also assume passing a copy of a complex object would be far more expensive than passing a reference.
Destroyed as out of scope when the function terminated?
Yes.

I also assume passing a copy of a complex object would be far more expensive than passing a reference.
"far" might be an exaggeration. I'm guessing that your objects aren't too big. Also, dereferencing a pointer (or reference) isn't free either. Though generally you should pass large structs by (often const, if you're not going to change it) reference or pointer.
Topic archived. No new replies allowed.