Decorators the C++ way

We have encountered two problems while implementing a classic decorator in C++—first of all, the decorated object did not take ownership of the original object, so both must be kept around (this may not be so much a problem as a feature, if the decoration needs to be removed later, which is one of the reasons the decorator pattern is implemented this way). The other problem is that a decorated Knight is not really a Knight at all, but a Unit. We can solve the second problem if the decorator is itself derived from the class that is being decorated. This would imply that the VeteranUnit class does not have a fixed base class—the base class should be whatever class is being decorated. This description matches the Curiously ...

Get Hands-On Design Patterns with 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.