Can someone explain/give me an example of how the & symbol is used in C++. I looked at a couple of tutorials and read up on it but still dont understand. Thanks!
Pass-by-reference allows one to modify the value of what was passed to the function.
Your confusion may arise from the fact that the & symbol is also used in other contexts apart from pass-by-reference.
It is used as an address-of operator when we do:
1 2
int myInt = 42;
int *ptr = &myInt;
This makes ptr point to the address of the variable myInt.
When we do lambda expressions to capture references to variables within the current scope:
1 2 3 4 5 6 7 8 9 10
{
int myInt = 42;
int *ptr = &myInt;
auto myFunc = [&]() -> void {
*ptr = 9001;
std::cout << myInt << std::endl;
};
myFunc(); // 9001
}
These are the only uses I've found, but there might be others. All you need to do is to pay attention to how it's being used to know what behaviour to expect.