|
|
The c_string object provides a less dangerous way to manage C style strings (as char* arrays). The c_string object also provides member functions that allow you to manipulate strings in ways that were not possible using character arrays. The c_string object performs length tracking. This means that with repeated use, the internal buffer for the c_string can grow, but will never shrink. This provides a performance boost, as the c_string doesn't need to perform reallocations as often as it would if it allocated memory for each operaton that modified the length of the buffer. A side effect of this is that the c_string object will use a bit more memory, the extra memory usage is slight, and the performance benefit is great. |
|
|
void use_string_like_buffer( void ) { c_string str; str = "this is a string "; str += __FILE__; // the output of these two lines of code should be the same, // because strlen takes a const char*, and the c_string will // be autocasted to const char* for the call to strlen cout << "length of string is: " << str.get_length() << endl; cout << "length of string is: " << strlen(str) << endl; c_string file_name(__FILE__); ifstream ifile; ifile.open( file_name ); // ifstream::open takes a const char* ifile.close(); } |