O'Reilly logo

C++ Primer Plus by Stephen Prata

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required