C++ What does it mean when you pass by reference a function? Example:
int & modify(){}
same question with pointers. Example:
int * modify(){}
Alias:
Alias means another name of an existing variable. In C++, the reference variable is an alias.
For example:
int x;
int &y = x;
In the above statement, 'y' is an alias of variable 'x'.
Pass By Reference
When we pass the parameter as a reference, then the reference of the parameter is passed to the called function. The reference variable uses the pointer for implementation. A reference is the same object but a different name and it can't be assigned null. A reference variable can be used directly and there is no need for dereferencing operator '*'.
For example:
//method to return the reference of a variable
int & modify()
{
return a;
}
In the above method, we return the reference of a variable.
When we want to make changes in the actual parameter then we should use the reference variable.
By using pointer:
int * modify()
{
}
In the above example, the address is return and we need the dereferenced operator to get the value.
Difference between pointer and reference:
C++ What does it mean when you pass by reference a function? Example: int & modify(){}...