How it works...

In this recipe, we will learn how to implement the decorator pattern. To start, let's look at a simple example: suppose we are writing a C++ application that will host a website. In our website, we need to define a button that users can click on, but we need to calculate the width of the button given an extra margin that adds to the total size of the button:

class margin{public:    int width()    {        return 32;    }};

As shown in the preceding, we have created a class called margin that returns the width of the margin in question (we will only focus on the width to simplify our example). We can then define our button as follows:

class button : public margin{public:    int width()    {        return margin::width() + 10;    }};

As shown in the preceding ...

Get Advanced C++ Programming Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.