Further Assignment Operator Overloading

Before looking at the new listings for the String class example, let’s consider another matter. Suppose you want to copy an ordinary string to a String object. For example, suppose you use getline() to read a string and you want to place it in a String object. The class methods already allow you to do the following:

String name;char temp[40];cin.getline(temp, 40);name = temp;  // use constructor to convert type

However, this might not be a satisfactory solution if you have to do it often. To see why, let’s review how the final statement works:

1. The program uses the String(const char *) constructor to construct a temporary String object containing a copy of the string stored in temp. Remember from Chapter ...

Get C++ Primer Plus now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.