Robert Ryan wrote:
> can I ask about the parameters: are the actual parameters Swap(&a, &b);
> and the formal, the parameters being passed to - void Swap(int *x, int *y);
>
> Swap(&a, &b);
>
> void Swap(int *x, int *y);
>
> Swap(a, b);
>
> void Swap(int &x, int &y);
>
> Thomas Hruska <[EMAIL PROTECTED]> wrote: Robert
> Ryan wrote:
> > but why someone would want to mess with pointers when C++ can
> > handle the majority of that nonsense* behind the scenes is somewhat
> > beyond me.
> > I thought that pass by reference in C++ used pointers
>
> Pass "by value" (won't actually swap a and b):
>
> Swap(a, b);
>
> void Swap(int x, int y);
>
> Pass "by reference":
>
> Swap(&a, &b);
>
> void Swap(int *x, int *y);
>
> Swap(a, b);
>
> void Swap(int &x, int &y);
>
> You can use all three in C++. Just the first two in C. The pointer
> operations in the third are transparent and done completely behind the
> scenes.
void Swap(int *x, int *y)
{
int t = *x;
*x = *y;
*y = t;
}
void Swap(int &x, int &y)
{
int t = x;
x = y;
y = t;
}
Both functions are identical except for how they are called. You call
the first:
Swap(&a, &b);
And the second:
Swap(a, b);
Some people say the former method is more "readable" but given that most
IDEs have all sorts of features these days, the point is fairly moot.
Hovering over the name of a function in VC++, for instance, reveals the
full prototype of that function, which makes it really easy to tell that
a and b are going to be modified without actually viewing the source for
Swap().
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/