What does it mean to pass a &variable to a function? E.g., string& insert ( size_t pos1, const strin

Posted by Bob Montgomery on Stack Overflow See other posts from Stack Overflow or by Bob Montgomery
Published on 2010-06-17T16:17:54Z Indexed on 2010/06/17 16:23 UTC
Read the original article Hit count: 228

Filed under:
|
|
|

I understand passing a pointer, and returning a pointer:

char * strcat ( char * destination, const char * source );

You're passing a variable that contains the address to a char; returning the same.

But what does it mean to pass something using the reference operator? Or to return it?

string& insert ( size_t pos1, const string& str );

I mean, I understand what actually happens, I just don't understand the notation. Why isn't the notation this instead:

string * insert ( size_t pos1, const string * str ); //made up

I presume it has something to do with passing/returning the instance of a class, but what? Is this syntax valid; if not why not and if so what does it mean?

char & strcat ( char & destination, const char & source ); //made up

(all of the function declarations, except the last made-up two, are from http://www.cplusplus.com )

© Stack Overflow or respective owner

Related posts about c++

Related posts about class