Chapter 15. Lazy Optimization, Part 3: Iterators and References

Difficulty: 6

In the third part of this miniseries, we examine the effect of references and iterators into a copy-on-write string. Can you spot the issues?

Consider the copy-on-write Optimized::String class from Item 14, but with two new functions: Length() and operator[]().

 namespace Optimized { class StringBuf { public: StringBuf(); // start off empty ~StringBuf(); // delete the buffer void Reserve( size_t n ); // ensure len >= n char* buf; // allocated buffer size_t len; // length of buffer size_t used; // # chars actually used unsigned refs; // reference count private: // No copying... // StringBuf( const StringBuf& ); StringBuf& operator=( const StringBuf& ); }; class String { ...

Get More Exceptional C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.