Tools.h++ Manual
104011 Tandem Computers Incorporated 19-3
19
On the surface, this approach is appealing because only one copy of the string
need be made. Hence, calling
setForeground()
is efficient. But, the
resulting semantics can be muddled: what if the string pointed to by
foreground
changes? Should the foreground color change? If so, how will
class
Simple
know of the change? There is also a maintenance problem:
before you can delete a "color" string, you must know if anyone is still pointing
to it.
Here's a much easier approach:
Now the assignment "
foreground=c
" will use value semantics. The color that
class
Smart
should use is completely unambiguous. It's efficient too, because
a copy of the data will not be made unless the string should change:
19.2 More on storing and retrieving RWCollectables
In Chapter 16, “Persistence” we saw how to use the global functions
RWvostream& operator<<(RWvostream&, const
RWCollectable&);
RWFile& operator<<(RWFile&, const
RWCollectable&);
RWvostream& operator<<(RWvostream&, const
RWCollectable*);
RWFile& operator<<(RWFile&, const
RWCollectable*);
RWvistream& operator>>(RWvistream&, RWCollectable&);
class Smart {
RWCString foreground;
RWCString background;
public:
setForeground(const RWCString& c) {foreground=c;}
setBackground(const RWCString& c) {background=c;}
Smart window;
RWCString color("white");
window.setForeground(color);// Two references to white
color = "Blue";// One reference to white, one to blue