Q&A
Q1: | Why have pointers if references are easier? |
A1: | References cannot be null, and they cannot be reassigned. Pointers offer greater flexibility, but are slightly more difficult to use. |
Q2: | Why would you ever return by value from a function? |
A2: | If the object being returned is local, you must return by value or you will be returning a reference to a nonexistent object. |
Q3: | Given the danger in returning by reference, why not always return by value? |
A3: | There is far greater efficiency in returning by reference. Memory is saved, and the program runs faster. |
Get Sams Teach Yourself C++ in 24 Hours, Third Edition 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.