Friday, October 1, 2010

Pass by Pointer vs. Pass by Reference

Even though you can pass an object by reference as a reference, if you want to change the object in a function; there is a convention of making the parameter a pointer to the object instead of a reference to the object to emphasize the fact that the object is an output parameter.

This is only a convention that some people use. The nice thing about this convention is that an output parameter is made obvious in the caller and the callee. You can argue that lack of 'const' may tell you it is an output parameter from the callee side, but: 1) it won't be shown on the caller side; and 2) parameters passed by value typically do not use the 'const' specifier.

No comments:

Post a Comment