Sunday, August 2, 2009

When are pointers pass by value and when are they pass by reference in a c++ function?

Passing by value and passing by reference are the same for pointers as for ordinary variables.





When passing a pointer by value, you're actually passing the address that the pointer points to.





When passing a pointer by reference, you're passing the address of the pointer, which itself is the address of some other item.





Passing by reference allows you to change the location that the pointer points to whereas by value just lets you use that address - it'll have no effect on the pointer when the function returns.

When are pointers pass by value and when are they pass by reference in a c++ function?
Pass by value when you want to retain the original value regardless of changes to the value inside the function.


Pass by reference when you require the original value to be changed.


No comments:

Post a Comment