I l @ ve RuBoard |
Difficulty: 2 Copy-on-write (also called "lazy copy") is a common optimization that uses reference counting. Do you know how to implement it? In this first Item, we consider a simple plain- vanilla String that's not reference-counted at all. In the rest of the miniseries, we'll see what impact adding copy-on-write semantics can have on the class. Consider the following simplified String class. (Note: This is not intended to be a complete string facility. It's been distilled to serve as an example, and is missing many common operations that you would otherwise see in a real string class. In particular, I don't show operator=() because its code is essentially the same as that of copy construction.) namespace Original { class String { public: String(); // start off empty ~String(); // free the buffer String( const String& ); // take a full copy void Append( char ); // append one character // ... operator=() etc. omitted ... private: char* buf_; // allocated buffer size_t len_; // length of buffer size_t used_; // # chars actually used }; } This is a simple String that does not contain any fancy optimizations. When you copy an Original::String , the new object immediately allocates its own buffer and you immediately have two completely distinct objects. Your assignment: Implement Original::String . |
I l @ ve RuBoard |