0

I know the & operator means address-of, but what's it do when in a function prototype?

Example:

void foo(std::string& str)

or

void foo(char& c)

what's the & for in that?

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by VernonDozier
0

I know the & operator means address-of, but what's it do when in a function prototype?

Example:

void foo(std::string& str)

or

void foo(char& c)

what's the & for in that?

The & symbol means that you are passing the parameter to the function by "reference" rather than by "value". When the & symbol is next to a parameter, if you change that parameter in the function, that will change the variable that was passed to it when the function was called.

For example,

void foo(char& c)
{
     c = 'a';
}

If the code below was executed:

char x;
foo (x);
cout << x;

the output would be this:

a
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.