The second line here uses the overloaded + operator to create a temporary string object, which is then assigned, using the overloaded = operator, to the four object. As you might expect, the + operator concatenates its two operands into a single string object. The operator is multiply overloaded, so the second operand can be a string object or a C-style string or a char value.
The fifth constructor takes a C-style string and an integer as arguments, with the integer indicating how many characters to copy:
char alls[] = "All's well that ends well";
string five(alls,20); // ctor #5
Here, as the output shows, just the first 20 characters ("All's well that ends") are used to initialize the five object. As Table 16.1 notes, if the character count exceeds the length of the C-style string, the requested number of characters is still copied. So replacing 20 with 40 in the preceding example would result in 15 junk characters being copied at the end of five. (That is, the constructor would interpret the contents in memory following the string "All's well that ends well" as character codes.)
The sixth constructor has a template argument:
template
The intent is that
string six(alls+6, alls + 10); // ctor #6
Because the name of an array is a pointer, both alls + 6 and alls + 10 are type char *, so the template is used with Iter replaced by type char *. The first argument points to the first w in the alls array, and the second argument points to the space following the first well. Thus, six is initialized to the string "well". Figure 16.1 shows how the constructor works.
Figure 16.1. A string constructor using a range.
Now suppose you want to use this constructor to initialize an object to part of another string object—say, the object five. The following does not work:
string seven(five + 6, five + 10);
The reason is that the name of an object, unlike the name of an array, is not treated as the address of an object, hence five is not a pointer and five + 6 is meaningless. However, five[6] is a char value, so &five[6] is an address and can be used as an argument to the constructor:
string seven(&five[6], &five[10]);// ctor #6 again
The seventh constructor copies a portion of one string object to the constructed object:
string eight(four, 7, 16); // ctor #7
This statement copies 16 characters from four to eight, starting at position 7 (the eighth character) in four.
C++11 Constructors
The string(string && str) noexcept constructor is similar to the copy constructor in that the new string is a copy of str. However, unlike the copy constructor, it doesn’t guarantee that str will be treated as const. This form of constructor is termed a
The string(initializer_list
string piano_man = {'L', 'i', 's','z','t'};
string comp_lang {'L', 'i', 's', 'p'};
This may not be that useful for the string class because using C-style strings is easier, but it does satisfy the intent to make the list-initialization syntax universal. This chapter will discuss the initializer_list template further later on.
The string Class Input
Another useful thing to know about a class is what input options are available. For C-style strings, recall, you have three options:
char info[100];
cin >> info; // read a word
cin.getline(info, 100); // read a line, discard \n
cin.get(info, 100); // read a line, leave \n in queue
For string objects, recall, you have two options:
string stuff;
cin >> stuff; // read a word
getline(cin, stuff); // read a line, discard \n
Both versions of getline() allow for an optional argument that specifies which character to use to delimit input:
cin.getline(info,100,':'); // read up to :, discard :
getline(stuff, ':'); // read up to :, discard :