Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does '&' do in a C++ declaration?

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++?

like image 563
poundifdef Avatar asked Dec 21 '09 23:12

poundifdef


People also ask

What does the fox say Meaning?

Speaking of the meaning of the song, Vegard characterizes it as coming from "a genuine wonder of what the fox says, because we didn't know". Although interpreted by some commentators as a reference to the furry fandom, the brothers have stated they did not know about its existence when producing "The Fox".

How can I find a song by the sound?

On your phone, touch and hold the Home button or say "Hey Google." Ask "What's this song?" Play a song or hum, whistle, or sing the melody of a song. Hum, whistle, or sing: Google Assistant will identify potential matches for the song.

What does the fox say for real?

One of the most common fox vocalizations is a raspy bark. Scientists believe foxes use this barking sound to identify themselves and communicate with other foxes. Another eerie fox vocalization is a type of high-pitched howl that's almost like a scream.


2 Answers

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

foo(string const& myname)  

over

foo(string const* myname) 

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:

const string &GetMethodName() { ... } 

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:

class A {   public:   int bar() const {return someValue;}   //Big, expensive to copy class }  class B { public:  A const& getA() { return mA;} private:  A mA; } void someFunction() {  B b = B();  //Access A, ability to call const functions on A  //No need to check for null, since reference is guaranteed to be valid.  int value = b.getA().bar();  } 

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)

int const& foo()  {  int a;   //This is very bad, returning reference to something on the stack. This will  //crash at runtime.  return a;  } 

Basically, it is your responsibility to ensure that whatever you are returning a reference to is actually valid.

like image 139
czuger Avatar answered Sep 21 '22 20:09

czuger


Here, & is not used as an operator. As part of function or variable declarations, & denotes a reference. The C++ FAQ Lite has a pretty nifty chapter on references.

like image 32
Victor Nicollet Avatar answered Sep 17 '22 20:09

Victor Nicollet