This makes jolly a reference to the new structure. There is a problem with this approach: You should use delete to free memory allocated by new when the memory is no longer needed. A call to clone() conceals the call to new, making it simpler to forget to use delete later. The auto_ptr template or, better, the C++11 unique_ptr discussed in Chapter 16, “The string Class and the Standard Template Library,” can help automate the deletion process.
Why Use const with a Reference Return?
Listing 8.6, as you’ll recall, had this statement:
accumulate(dup,five) = four;
It had the effect of first adding data from five to dup, then overwriting the contents of dup with the contents of four. Why does this statement compile? Assignment requires a modifiable lvalue on the left. That is, the subexpression on the left of an assignment expression should identify a block of memory that can be modified. In this case, the function returned a reference to dup, which does identify such a block of memory. So the statement is valid.
Regular (non reference) return types, on the other hand, are
Suppose you want to use a reference return value but don’t want to permit behavior such as assigning a value to accumulate(). Just make the return type a const reference:
const free_throws &
accumulate(free_throws & target, const free_throws & source);
The return type now is const, hence a nonmodifiable lvalue. Therefore, the assignment no longer is allowed:
accumulate(dup,five) = four; // not allowed for const reference return
What about the other function calls in the program? With a const reference return type, the following statement would still be allowed:
display(accumulate(team, two));
That’s because the formal parameter for display() also is type const free_thows &. But the following statement would not be allowed because the first formal parameter for accumulate() is not const:
accumulate(accumulate(team, three), four);
Is this a great loss? Not in this case because you still can do the following:
accumulate(team, three);
accumulate(team, four);
And of course you still could use accumulate() on the right side of an assignment statement.
By omitting const, you can write shorter but more obscure-looking code.
Usually, you’re better off avoiding the addition of obscure features to a design because obscure features often expand the opportunities for obscure errors. Making the return type a const reference therefore protects you from the temptation of obfuscation. Occasionally, however, omitting const does make sense. The overloaded << operator discussed in Chapter 11, “Working with Classes,” is an example.
Using References with a Class Object
The usual C++ practice for passing class objects to a function is to use references. For instance, you would use reference parameters for functions taking objects of the string, ostream, istream, ofstream, and ifstream classes as arguments.
Let’s look at an example that uses the string class and illustrates some different design choices, some of them bad. The general idea is to create a function that adds a given string to each end of another string. Listing 8.7 provides three functions that are intended to do this. However, one of the designs is so flawed that it may cause the program to crash or even not compile.
Listing 8.7. strquote.cpp
// strquote.cpp -- different designs
#include
#include
using namespace std;
string version1(const string & s1, const string & s2);
const string & version2(string & s1, const string & s2); // has side effect
const string & version3(string & s1, const string & s2); // bad design
int main()
{
string input;
string copy;
string result;
cout << "Enter a string: ";
getline(cin, input);
copy = input;
cout << "Your string as entered: " << input << endl;
result = version1(input, "***");
cout << "Your string enhanced: " << result << endl;
cout << "Your original string: " << input << endl;
result = version2(input, "###");
cout << "Your string enhanced: " << result << endl;
cout << "Your original string: " << input << endl;
cout << "Resetting original string.\n";
input = copy;
result = version3(input, "@@@");
cout << "Your string enhanced: " << result << endl;
cout << "Your original string: " << input << endl;
return 0;
}
string version1(const string & s1, const string & s2)
{
string temp;
temp = s2 + s1 + s2;
return temp;
}
const string & version2(string & s1, const string & s2) // has side effect
{
s1 = s2 + s1 + s2;