The & here means a Reference.
It is just an alias to the variable being passed.
Any modification performed on b inside the function will result in modification of the Object being passed to the function.
Without the & an copy of the object will be passed to the function and any modification performed on it inside the function will not modify the Original Object.
Detailed explanation:
Consider the simple example below, it demonstrates the 3 ways in which you can pass arguments to a function:
Pass By value:
void passByValue(MyClass obj)
When an function argument is passed by value, the function receives an copy of the variable being passed, any modification done on this copy inside the function body does not affect the original variable being passed.
Pass an Reference:
void passAnRef(MyClass &ref)
The function receives an alias to the variable being passed.Any modification performed on this alias modify's the original variable as well. If the argument being passed is an custom class object then you can access the class members just like accesing through an object by using the . operator.
Pass an pointer:
void passAnPtr(MyClass *ptr)
The function receives an pointer to the original variable being passed. i.e: It receives an variable which points to address of original variable in memory. Any modification performed through this pointer modify's the original object since it just points to that memory.
If the argument being passed is an custom class object then you can access the class members just like accessing through an pointer to object by using the -> operator.
Online Demo:
#include<iostream> using namespace std; class MyClass { public: int i; MyClass():i(0){} }; void passByValue(MyClass obj) { obj.i = obj.i + 10; } void passAnRef(MyClass &ref) { ref.i = ref.i + 10; } void passAnPtr(MyClass *ptr) { ptr->i = ptr->i + 10; } int main() { MyClass obj; cout<<"Before passByValue obj is:"<<obj.i; passByValue(obj); cout<<"\nAfter passByValue obj is:"<<obj.i; cout<<"\nBefore passAnRef obj is:"<<obj.i; passAnRef(obj); cout<<"\nAfter passAnRef obj is:"<<obj.i; cout<<"\nBefore passAnPtr obj is:"<<obj.i; passAnPtr(&obj); cout<<"\nAfter passAnPtr obj is:"<<obj.i; return 0; }
Output:
Before passByValue obj is:0
After passByValue obj is:0
Before passAnRef obj is:0
After passAnRef obj is:10
Before passAnPtr obj is:10
After passAnPtr obj is:20