C++ Mixins - Reuse through inheritance is good... when done the right way

Conventional Wisdom

Conventional wisdom tells us that code reuse through inheritance is evil. Go ahead and Google it if you're not clear on the stance in the community. The majority of articles that I've read encourage the programmer to favour composition and delegation (for example, by using the strategy pattern) over inheritance in order to attain code reuse. In general this advice is good as code-reuse-through-inheritance (as it is commonly implemented) does suffer from many problems. However, composition-and-delegation suffers from its own set of problems... So is there a better approach?

In this article I introduce the concept of the C++ Mixin. It will be shown that code reuse through inheritance in C++ mixins is not evil - in fact, it has so many advantages that it should be a technique commonly employed by C++ programmers.

Tutorials on C++ mixins tend to be a bit confusing and use contrived examples that make you wonder why you'd bother with this obsure technique. The intention with this article is to provide a easy to understand introduction to Mixins, their implementation and a comparison to other techniques for code reuse.

Subscribe to RSS - Mixins