Parameter passing guidelines:
Exceptions:
getNext(...), getText(...)
The following is a demonstration of various parameter passing methods:
7#7
PrintMyStructOne(), the naive implementation, copies the
CMyStruct data into its parameter ms, thus it copies 15,000
bytes of data. It is not desirable to copy this much data when
it is not necessary.
PrintMyStructTwo() does not copy data, but when used, as in
main() above, you must pass it a pointer. Passing a pointer
implies that the data object could very well be modified within the
function that is being called. Even though the parameter is a const in
the function declaration, it is not clear from the perspective of
main() that the function being called does not modify the
object. The body of PrintMyStructTwo() is also slightly more
complicated. Now you must deal with pointers and de-referencing
within your function.
PrintMyStructThree() does not copy data, and because you do not
pass it a pointer to the object, it does not imply that the function
will be changing the passed object. Note that the body of
PrintMyStructThree() and the naive method
PrintMyStructOne() are the same. Also Note that the call in
main() to PrintMyStructThree() looks exactly the same as
the call to the naive method PrintMyStructOne(). It is just as
simple to use as the naive method and looks cleaner than the pointer
approach in PrintMyStructTwo().