What is C++ Mixin-Style?

Mixins are a concept from Lisp. A good explanation from Dr. Dobbs:

A mixin is a fragment of a class in the sense that it is intended to be composed with other classes or mixins.
[…]
The difference between a regular, stand-alone class (such as Person) and a mixin is that a mixin models some small functionality slice (for example, printing or displaying) and is not intended for standalone use. Rather, it is supposed to be composed with some other class needing this functionality (Person, for instance).

So, the point of a mixin is to allow something like multiple-inheritance, without all the Bad Thingsā„¢ that usually come along with multiple inheritance.

This can be a bit confusing, however, because C++ does not natively support mixins; in order to “do” mixins in C++, you have to use multiple-inheritance! What this ends up meaning in practice is that you still use multiple-inheritence, but you artifically limit what you allow yourself to use it for.

See the article above for an actual mixin implementation.

Leave a Comment