Sign in to follow this  
DaMaster_Architect

Question regarding pointers in functions/classes

Recommended Posts

hi all, I am currently learning C++. For that purpose, I bought the book "C++ for dummies". So far, this book explains everything well. However, in the chapter about classes, (page 169 for the ones interested) there is a piece of code displayed, with a function in it. Here's the piece of code:
class Savings
{
public:
 unsigned accountNumber;
 float balance;
};
float deposit(Savings& s, unsigned amount)
{
 s.balance += amount;
 return s.balance;
}
so far, what I understand from the code is that a class savings is created, and that one of its members is changed in the function deposit. But what does the argument Savings& s do? Does it recieve the address of a variable? I don't quite understand this argument. I would appriciate it a lot if someone could explain it to me. Thanks in advance!

Share this post


Link to post
Share on other sites
Conner McCloud    1135
You're pretty much right about what it does. The & signifies that the object is being passed in by reference...this means that any changes made to that object will show up outside of the function. This is as opposed to passing an object by value, which results in a copy of the object being made. This copy can be modified freely, the original object won't be affected.

This is most easily visualized with an example.

#include <iostream>
using namespace std;

void by_ref(int& i)
{
cout << "Inside by_ref: " << i << endl;
i++;
}

void by_val(int i)
{
cout << "Inside by_val: " << i << endl;
i++;
}

int main()
{
int i = 10;
cout << "Initial value: " << i << endl;
by_ref(i);
cout << "After by_ref: " << i << endl;
by_val(i);
cout << "After by_val: " << i << endl;
}


That should output the following:
Quote:

Initial value: 10
Inside by_ref: 10
After by_ref: 11
Inside by_val: 11
After by_val: 11

Note that i changed from 10 to 11 by calling by_ref, but stayed 11 when calling by_val. This is because in the latter case, i got copied, and this copy got incremented instead of the original.

I hope that clears things up.

CM

Share this post


Link to post
Share on other sites
PiCkLeD    188

class Savings
{
public:
unsigned accountNumber;
float balance;
};

float deposit(Savings& s, unsigned amount)
{
//s.balance refers to 'example.balance' as created in 'main'
s.balance += amount;

//return the result of 'example.balance' to float 'result'
return s.balance;
}

void main(void)
{
//Our float result
float result;

//Create a new 'Savings' object called 'example'
Savings example;

//'example' object is passed to the function 'deposit' by reference
//.. this means that 'example' will be modified directly in the function
result = deposit(example, 300);

//result == example.balance == 300
}




But now that you see your code explained, study Conner's execellent example since it clearly outlines it.

Share this post


Link to post
Share on other sites
PiCkLeD    188
You may want to consider changing the object from a function defined in the class itself!

class Savings
{
public:
void deposit(float amount);
private:
float balance;
};

Savings example;

example.deposit(300);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this