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 O’Reilly online learning.

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