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 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.