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 << "called foo for: " << myname << endl;
return 0;
}
How does the function signature differ from the equivalent C:
int foo(const char *myname)
Is there a difference between using string *myname
vs string &myname
? What is the difference between &
in C++ and *
in C to indicate pointers?
Similarly:
const string &GetMethodName() { ... }
What is the &
doing here? Is there some website that explains how &
is used differently in C vs C++?
Best Answer
The "&" denotes a reference instead of a pointer to an object (In your case a constant reference).
The advantage of having a function such as
over
is that in the former case you are guaranteed that myname is non-null, since C++ does not allow NULL references. Since you are passing by reference, the object is not copied, just like if you were passing a pointer.
Your second example:
Would allow you to return a constant reference to, for example, a member variable. This is useful if you do not wish a copy to be returned, and again be guaranteed that the value returned is non-null. As an example, the following allows you direct, read-only access:
You have to of course be careful to not return invalid references. Compilers will happily compile the following (depending on your warning level and how you treat warnings)
Basically, it is your responsibility to ensure that whatever you are returning a reference to is actually valid.