What is C++ Mixin-Style?

Bitmap picture Bitmap · Aug 16, 2011 · Viewed 21.3k times · Source

I have just come across this keyword C++ Mixin-Style, do anyone know what this is?

In this post, is has been answered as a design pattern. Is it the same design pattern as described in this document?

Answer

BlueRaja - Danny Pflughoeft picture BlueRaja - Danny Pflughoeft · Aug 16, 2011

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.