Search code examples
c++arraysfunctionampersand

C++ ampersand in function


I want to know that these definitions of functions are equal, ie there is no difference?

void fun1(int **X){....}
void fun2(int **&X){....}

int main(){
.....
fun1(Array);
fun2(Array);
.....
}

Or it is something change?


Solution

  • The definition is obviously different, and the difference in functionality will greatly depend on how the functions are implemented. Passing a parameter to fun2 and then writing to the parameter inside the body of the function will write to the referenced memory location, while passing the same parameter to fun1 and writing to it inside the body of the function will leave the original parameter intact, since the passed parameter will just be a copy in a different memory location.