3.2.1. Defining and Initializing strings

Image

Each class defines how objects of its type can be initialized. A class may define many different ways to initialize objects of its type. Each way must be distinguished from the others either by the number of initializers that we supply, or by the types of those initializers. Table 3.1 lists the most common ways to initialize strings. Some examples:

string s1;            // default initialization; s1 is the empty stringstring s2 = s1;       // s2 is a copy of  s1string s3 = "hiya";   // s3 is a copy of the string literalstring s4(10, 'c');   // s4 is cccccccccc

Table 3.1. Ways to Initialize a string

Get C++ Primer, Fifth Edition 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.