When to Use Reference Arguments
There are two main reasons for using reference arguments:
• To allow you to alter a data object in the calling function
• To speed up a program by passing a reference instead of an entire data object
The second reason is most important for larger data objects, such as structures and class objects. These two reasons are the same reasons you might have for using a pointer argument. This makes sense because reference arguments are really just a different interface for pointer-based code. So when should you use a reference? Use a pointer? Pass by value? The following are some guidelines.
A function uses passed data without modifying it:
• If the data object is small, such as a built-in data type or a small structure, ...
Get C++ Primer Plus now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.