Professional C__ - Marc Gregoire [418]
And you’re still not done. Remember that p in the first version of processPerson() is a local variable to the processPerson() function, and so must be destroyed when the function exits. This destruction requires a call to the Person destructor, which will call the destructor of all of the data members. strings have destructors, so exiting this function (if you passed by value) incurs calls to three destructors. None of those calls are needed if the Person object is passed by reference.
If a function must modify an object, you can pass the object by reference. If the function should not modify the object, you can pass it by const reference, as in the preceding example. See Chapter 9 for details on references and const.
Avoid using pass-by-pointer, which is a relatively obsolete method for pass-by-reference, and is a throwback to the C language, rarely suitable for C++ (unless passing nullptr has meaning in your design).
Return-by-Reference
Just as you should pass objects by reference to functions, you should also return them by reference from functions in order to avoid copying the objects unnecessarily. Unfortunately, it is sometimes impossible to return objects by reference, such as when you write overloaded operator+ and other similar operators. You should never return a reference or a pointer to a local object that will be destroyed when the function exits.
Catch Exceptions by Reference
As noted in Chapter 10, you should catch exceptions by reference in order to avoid an extra copy. Throwing exceptions is heavy in terms of performance, so any little thing you can do to improve their efficiency will help.
Use Move Semantics
You should implement a move constructor and move assignment operator for your objects, which allow the C++ compiler to use move semantics. With move semantics for your objects, returning them by value from a function will be efficient without incurring large copying costs. Consult Chapter 9 for details on move semantics.
Avoid Creating Temporary Objects
The compiler creates temporary, unnamed objects in several circumstances. Chapter 7 explains that after writing a global operator+ for a class, you can add objects of that class to other types, as long as those types can be converted to objects of that class. For example, the SpreadsheetCell class definition looks in part like the following:
class SpreadsheetCell
{
public:
// Other constructors omitted for brevity
SpreadsheetCell(double initialValue);
friend const SpreadsheetCell operator+(const SpreadsheetCell& lhs,
const SpreadsheetCell& rhs);
// Remainder omitted for brevity
};
The constructor that takes a double allows you to write code like this:
SpreadsheetCell myCell(4), aThirdCell;
aThirdCell = myCell + 5.6;
aThirdCell = myCell + 4;
The first addition line constructs a temporary SpreadsheetCell object from the 5.6 argument; then calls the operator+ with myCell and the temporary object as arguments. The result is stored in aThirdCell. The second addition line does the same thing, except that 4 must be coerced to a double in order to call the double constructor of the SpreadsheetCell.
The important point in this example is that the compiler generates code to create an extra, unnamed SpreadsheetCell object for each addition line. That object must be constructed and destructed with calls to its constructor and destructor. If you’re still skeptical, try inserting cout statements in your