Initializing a string Object

A string object can be initialized with a constructor argument as in

string text( "Hello" ); // creates a string from a const char *

which creates a string containing the characters in "Hello", or with two constructor arguments as in

string name( 8, 'x' ); // string of 8 'x' characters

which creates a string containing eight 'x' characters. Class string also provides a default constructor (which creates an empty string) and a copy constructor. A string also can be initialized in its definition as in

string month = "March"; // same as: string month( "March" );

Remember that = in the preceding declaration is not an assignment; rather it’s an implicit call to the string class constructor, which does the conversion. ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

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