I understand as with any other variable, the type of a parameter determines the interaction between the parameter and its argument. My question is that what is the reasonin
By reference is equally to manually pass the variable by pointer, but by reference will not let user to handle the "easy to mess up" pointer.
The ability to pass by reference exists for two reasons:
Example for modifying the argument
void get5and6(int *f, int *s) // using pointers
{
*f = 5;
*s = 6;
}
this can be used as:
int f = 0, s = 0;
get5and6(&f,&s); // f & s will now be 5 & 6
OR
void get5and6(int &f, int &s) // using references
{
f = 5;
s = 6;
}
this can be used as:
int f = 0, s = 0;
get5and6(f,s); // f & s will now be 5 & 6
When we pass by reference, we pass the address of the variable. Passing by reference is similar to passing a pointer - only the address is passed in both cases.
For eg:
void SaveGame(GameState& gameState)
{
gameState.update();
gameState.saveToFile("save.sav");
}
GameState gs;
SaveGame(gs)
OR
void SaveGame(GameState* gameState)
{
gameState->update();
gameState->saveToFile("save.sav");
}
GameState gs;
SaveGame(&gs);
Also, read on const
references. When it's used, the argument cannot be modified in the function.
This article helped me a lot.
Please forget about pointers for now. And take this with a grain of salt.
A reference is the object. When you pass by reference, you pass the object.
When you pass by value, you pass a copy of the object; another object. It may have the same state, but it is a different instance; a clone.
So, it may make sense to pass by reference if you:
const
reference.And it may make sense to pass by value if you:
int
by reference unless I wanted to modify it).Here, have a look at this code:
#include<iostream>
struct Foo {
Foo() { }
void describe() const {
std::cout<<"Foo at address "<<this<<std::endl;
}
};
void byvalue(Foo foo) {
std::cout<<"called byvalue"<<std::endl;
foo.describe();
}
void byreference(Foo& foo) {
std::cout<<"called byreference"<<std::endl;
foo.describe();
}
int main() {
Foo foo;
std::cout<<"Original Foo"<<std::endl;
foo.describe();
byreference(foo);
byvalue(foo);
}
And compile it like this: g++ example.cpp
Run it: ./a.out
And check the output (the actual addresses may be different in your computer, but the point will remain):
Original Foo
Foo at address 0x7fff65f77a0f
called byreference
Foo at address 0x7fff65f77a0f
called byvalue
Foo at address 0x7fff65f779f0
Notice how the called byreference
address is the same as the Original Foo
address (both are 0x7fff65f77a0f
). And notice how the called byvalue
address is different (it is 0x7fff65f779f0
).
Take it up a notch. Modify the code to look as follows:
#include<iostream>
#include<unistd.h> // for sleeping
struct Foo {
Foo() { }
Foo(const Foo&) {
sleep(10); // assume that building from a copy takes TEN seconds!
}
void describe() const {
std::cout<<"Foo at address "<<this<<std::endl;
}
};
void byvalue(Foo foo) {
std::cout<<"called byvalue"<<std::endl;
foo.describe();
}
void byreference(Foo& foo) {
std::cout<<"called byreference"<<std::endl;
foo.describe();
}
int main() {
Foo foo;
std::cout<<"Original Foo"<<std::endl;
foo.describe();
byreference(foo);
byvalue(foo);
}
Compile it the same way, and notice the output (comments not in the output; included for clarity):
Original Foo
Foo at address 0x7fff64d64a0e
called byreference
Foo at address 0x7fff64d64a0e # this point is reached "immediately"
called byvalue # this point is reached TEN SECONDS later
Foo at address 0x7fff64d64a0f
So, the code is meant to exaggerate the cost of a copy: when you called by reference this cost was NOT incurred. When you called by value you had to wait for ten seconds.
Note: my code was compiled in OS X 10.7.4 using GCC 4.8.1. If you are in windows you may need something different from unitsd.h
to make the sleep
call work.
Maybe this helps.
Pros of using pass by reference: you don't have to create a copy of the data that you are passing just the pointer to it in memory. (huge performance win think if you had a massive object that you passed around). You can "return" multiple values I know some functions in c/c++ return a number and one of the params is a pointer to the data that get's manipulated.
Cons of using pass by reference: you have to be careful modifying the data passed as it could cause side effects that you may or may not want.