© Dmitri Nesteruk 2018
Dmitri NesterukDesign Patterns in Modern C++https://doi.org/10.1007/978-1-4842-3603-1_9

9. Decorator

Dmitri Nesteruk1 
(1)
St. Petersburg, Russia
 

Suppose you’re working with a class your colleague wrote, and you want to extend that class’ functionality. How would you do it, without modifying the original code? Well, one approach is inheritance: you make a derived class, add the functionality you need, maybe even override something, and you’re good to go.

Right, except this doesn’t always work, and there are many reasons why. For example, you typically wouldn’t want to inherit from, say, std:: vector due to its lack of a virtual destructor, or from int (that’s just impossible). But the most critical reason why inheritance doesn’t ...

Get Design Patterns in Modern C++: Reusable Approaches for Object-Oriented Software Design 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.