Okay, so I just started learning C++ and I'm finding the ins and outs of argument passing to be a little confusing. I came across the following line in my C++ book;
In C++, passing by reference is accomplished in two ways: using pointers and using references.
It then goes on to show a program that switches the values of two int values, x and y (shown below). I was wondering, are the arguments to the swap()
function really passed by reference in this case? I thought they were being passed by address, which I've been told is just a special case of pass by value.
//Listing 9.6 Demonstrates passing by reference
#include <iostream.h>
void swap(int *x, int *y);
int main()
{
int x = 5, y = 10;
cout << "Main. Before swap, x: " << x
<< " y: " << y << "\n";
swap(&x,&y);
cout << "Main. After swap, x: " << x
<< " y: " << y << "\n";
return 0;
}
void swap(int *px, int *py)
{
int temp;
cout << "Swap. Before swap, *px: "
<< *px << " *py: " << *py << "\n";
temp = *px;
*px = *py;
*py = temp;
cout << "Swap. After swap, *px: " << *px
<< " *py: " << *py << "\n";
}