Originally posted by Campbell Ritchie:
I thought C and C++ were call-by-value too. But you can pass a pointer to a method, as you say, with the & operator.But I thought you are actually passing the value of the pointer, thereby mimicking pass-by-value. You can't alter the pointer, but you can alter its contents.
[edit]Last but one line should read "mimicking pass-by-reference."[/edit]
[ July 30, 2008: Message edited by: Campbell Ritchie ]
That's not quite what I meant. That's not using a reference but a pointer. Something that does not exist in Java.
In C++ (not C!) you can do the following:
This will print "x = 10, y = 5". Remove one or both &s and there is no more swapping.
Note how you do
not have to use any special syntax in the method call, just the method definition. A real improvement over the pointer syntax.