A Simple String Class


The standard C++ string class is a low-level class that contains most of the functionality found in the standard C library's string.h. The string class's key advantages are automatic memory management and the syntactic convenience of overloaded operators. Also, under the current definition, a C++ string may also contain embedded null bytes.

I will not publish the entire string class here. I include only the subset needed to implement the bitstring class (see Listing 1, Listing 2, and Listing 3) . Missing from my listing are functions such as insert, remove, replace and comparison operations. Mine is a straightforward "cloning" implementation, which makes a separate copy whenever one string is constructed from another (as opposed to a reference-counting scheme which copies only when a string is modified — see Chapter 3 of Jim Coplien's Advanced C++). I also do not allow embedded nulls in a string.