I am aware that both of the following are acceptable.
...header file...
class BigClass{
};
....outside....
BigClass BigObject;
void foo(BigClass& object); // a
void foo(BigClass* objectptr); // bPurely from a typing point of view, would not `a` be preferred? If I have to refer to a member function inside foo via option a, it is simply `object.function();` as opposed to the more cumbersome option b which yields `objectptr->function();`
I say cumbersome because typing `-` followed by `>` usually requires me to look at the keyboard and then 3 key presses (one of them being a shift key) while with a `.` it is much quicker.
Do professional programmers use some sort of a snippet expansion which makes the `->` flow much quicker?
Are there benefits of passing by pointer over passing by reference in C++? - Stack Overflow
When to use pointers and references? - C++ Forum
c++ - Pass by reference vs pass by pointer? - Stack Overflow
Pass by reference Vs Pointer
What is the primary difference between pass by reference and pass by pointer?
When should you use pass by pointer?
When should you use pass by reference?
Videos
Passing by pointer
- Caller has to take the address -> not transparent
- A 0 value can be provided to mean
nothing. This can be used to provide optional arguments.
Pass by reference
- Caller just passes the object -> transparent. Has to be used for operator overloading, since overloading for pointer types is not possible (pointers are builtin types). So you can't do
string s = &str1 + &str2;using pointers. - No 0 values possible -> Called function doesn't have to check for them
- Reference to const also accepts temporaries:
void f(const T& t); ... f(T(a, b, c));, pointers cannot be used like that since you cannot take the address of a temporary. - Last but not least, references are easier to use -> less chance for bugs.
A pointer can receive a NULL parameter, a reference parameter can not. If there's ever a chance that you could want to pass "no object", then use a pointer instead of a reference.
Also, passing by pointer allows you to explicitly see at the call site whether the object is passed by value or by reference:
// Is mySprite passed by value or by reference? You can't tell
// without looking at the definition of func()
func(mySprite);
// func2 passes "by pointer" - no need to look up function definition
func2(&mySprite);
When you pass a parameter by reference, the parameter inside the function is an alias to the variable you passed from the outside. When you pass a variable by a pointer, you take the address of the variable and pass the address into the function. The main difference is that you can pass values without an address (like a number) into a function which takes a const reference, while you can't pass address-less values into a function which takes const pointers.
Typically a C++ compiler implement a reference as a hidden pointer.
You can change your function into the pointer variant this way:
void flip(int *i) // change the parameter to a pointer type
{
cout << " flip start "<<"i="<< *i<<"\n"; // replace i by *i
*i = 2*(*i); // I'm not sure it the parenthesis is really needed here,
// but IMHO this is better readable
cout << " flip exit "<<"i="<< *i<<"\n";
}
int main()
{
int j =1;
cout <<"main j="<<j<<endl;
flip(&j); // take the address of j and pass this value
// adjust all other references ...
}
For the second part of your question, here is the code.
#include <iostream>
#include <cassert>
using namespace std;
void flip(int *i)
{
cout << " flip start "<<"i="<< i<<"\n";
*i *= 2;
cout << " flip exit "<<"i="<< i<<"\n";
}
int main()
{
int j =1;
cout <<"main j="<<j<<endl;
flip(&j);
cout <<"main j="<<j<<endl;
flip(&j);
cout <<"main j="<<j<<endl;
flip(&j);
cout <<"main j="<<j<<endl;
assert(j==8);
return 0;
}
For the first part of your question, I am new to C++ but I find it useful to pass by pointer when having to return multiple outputs for a function. Or to pass NULL as a parameter.