- 2.1. Our emphasis
- 2.2. The basic goal—a major difference between C++ and Java
- 2.3. Constructors and destructor
- 2.4. Operator overloading in C++
- 2.5. Operator overloading in Java
- 2.6. Flow-control constructs
- 2.7. Manipulating character strings in C++
- 2.8. Canonical class structure
- 2.9. Overcoming macrophobia
- 2.10. Program readability
- 2.11. Error detection and exceptions
2.7. Manipulating character strings in C++
This book is about numeric computing, not string manipulation. Therefore, we won’t discuss in detail techniques of string handling. However, our numeric classes will occasionally need to generate a character string—for example, as the result of a function that generates an external data representation.
2.7.1. C-style strings
Among popular procedural programming languages, C provided the weakest support for character strings. The usual way of representing a character string in C was as an array of char terminated by the nonprinting null character ASCII code 0, coded ‘\0’). Among the shortcomings that irritated C programmers were the following:
- A function couldn’t return a string (char*) result without introducing potentially catastrophic memory management anomalies. That made it impossible, for example, to write a usable substring function.
- Strings couldn’t be assigned (with the = operator) or compared (==, < operators) using ordinary expression syntax.
- A program could easily overrun the allocated space. A function had no way of determining how much space had been allocated to a char* parameter. This shortcoming alone led to innumerable subtle bugs and provided entree to some notorious virus programs.
- Except when the string length was a compile-time constant, the program had to allocate the memory explicitly. Then the actual data could not be contiguous with a record (struct), further complicating record copying and input-output.
2.7.2. User-defined string classes
C’s crude string capability inspired early C++ users to develop character-string classes or packages of related classes. Most of those string packages, including some from major compiler vendors, had serious flaws, ranging from catastrophic bugs to clumsy user interfaces to violations of object-oriented concepts. Some of them even preserved the shortcomings of C-style strings!
The minority that didn’t have serious flaws eventually brought first-rate string capabilities to C++ programmers in a few organizations, but none of them achieved widespread outside acceptance.
2.7.3. The standard string class
Eventually C++ got a string class that was blessed as a standard and supported by all modern compilers. As within C, strings are treated as containers rather than as elementary data items. The main virtue of this treatment are the extremely efficient implementations, usually based on reference counting.9 Nevertheless, there are some serious shortcomings:
- For simple and straightforward string handling, it’s harder to use than it ought to be, with an excessive number of methods having overlapping functionality.
- It provides no support for fixed-length strings, common in business forms and databases.
- It provides no support for embedded (contiguous) strings within a record, a traditional need in business applications.
2.7.4. String handling in this book
First, we’re going to avoid string handling wherever possible. For example, we’ll avoid implementing external input functions (istream& operator>> (...)) if we don’t absolutely need them.
However, our examples still need strings in several areas, especially external output or conversion to external representation. We have to use some string class, but none of the above, because
- although we know of some excellent, easy-to-use, user-defined string classes, it would be too burdensome for you to have to copy them and set them up
- the standard string class is too complicated and awkward to use for our simple needs
What we’re going to do, therefore, is to present the simplest possible string class, SimpleString. It permits
- string-valued functions
- string assignment
- concatenation
- size that is adjustable upon allocation
SimpleString doesn’t support scanning and parsing. Our SimpleString class is meant only to support examples in this book. You can run the examples and experiment with SimpleString, but you should choose a more complete and robust string class for your serious software development.