C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources]

Herb Sutter, Andrei Alexandrescu

نسخه متنی -صفحه : 521/ 152
نمايش فراداده

Examples

Example: An implementation of

+=

for strings. When concatenating strings, it is useful to know the length in advance so as to allocate memory only once.

String& String::operator+=( const String& rhs ) {

// … implementation … return *this; } String operator+( const String& lhs, const String& rhs ) { String temp;

// initially empty temp.Reserve( lhs.size() + rhs.size() );

// allocate enough memory return (temp += lhs) += rhs;

// append the strings and return }