I am a C guy and I\'m trying to understand some C++ code. I have the following function declaration:
int foo(const string &myname) {
cout << \"ca
In this context &
is causing the function to take stringname
by reference.
The difference between references and pointers is:
NULL
is not a valid value to a reference and will result in a compiler error. So generally, if you want to use an output parameter (or a pointer/reference in general) in a C++ function, and passing a null value to that parameter should be allowed, then use a pointer (or smart pointer, preferably). If passing a null value makes no sense for that function, use a reference.a
by writing b = 4;
. A reference's value is the value of whatever it referenced.