Tools.h++ Manual

104011 Tandem Computers Incorporated 19-1
Implementation Notes 19
19.1 Copy on write
Classes
RWCString
and
RWTValVirtualArray
use a technique called copy on
write to minimize copying. This technique offers the advantage of easy-to-
understand value semantics with the speed of reference counting.
When a
RWCString
is initialized with another
RWCString
via the copy
constructor
RWCString(const RWCString&);
then the two strings will share the same data until one of them tries to write to
it. At this point, a copy of the data is made and the two strings go their
separate ways. This makes copies, particularly read-only copies, of strings
very inexpensive. Consider the following example:
#include <rw/cstring.h>
RWCString g;// Global object
void setGlobal(RWCString x) { g = x; }
main()
{
RWCString a("kernel");// 1
RWCString b(a);// 2