Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is C++ Mixin-Style?

Tags:

c++

mixins

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?

like image 574
Bitmap Avatar asked Aug 16 '11 21:08

Bitmap


People also ask

What is a mixin type?

In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes.

What is a mixin method?

As defined in Wikipedia, a mixin is a class containing methods that can be used by other classes without a need to inherit from it. In other words, a mixin provides methods that implement a certain behavior, but we do not use it alone, we use it to add the behavior to other classes.

What is a mixin used for?

Mixins allow you to define styles that can be re-used throughout your stylesheet. They make it easy to avoid using non-semantic classes like . float-left , and to distribute collections of styles in libraries.

What is a mixin CSS?

A mixin lets you make groups of CSS declarations that you want to reuse throughout your site. It helps keep your Sass very DRY. You can even pass in values to make your mixin more flexible.


2 Answers

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.

like image 106
BlueRaja - Danny Pflughoeft Avatar answered Oct 02 '22 23:10

BlueRaja - Danny Pflughoeft


If I remember this correctly, there are at least two ways to create mixins in C++. This comes from some very old (1995) tutorial I've seen (but it's almost now completely disappeared from the internet).

First,

class MixinBase { public :     void f() {}; };  template<class T> class Mixin : public T { public:     void f() {         T::f();         T::f();     } };  template<class T> class Mixin2 : public T { public :     void g() {         T::f();         T::f();     } };  int main() {     Mixin2<Mixin<MixinBase>> mix;     mix.g(); } 

Or another way uses virtual inheritance, and sibling calls:

class Base { public :     virtual void f() = 0; };  class D1 : public virtual Base { public :     void g() {         f();     } };  class D2 : public virtual Base { public :     void f() {     } };  class D : public D1, public D2 { };  int main() {     D d;     d.g(); } 

Now these both versions implement mixins, because Mixin and Mixin2 are independent classes, but they can still communicate. And you can create software from this kind of modules and then later just bind those modules to one big software. Same happens between D1 and D2 in the virtual inheritance. Important thing to notice that in mixin design the different modules live inside the same c++ object. (oh and CRTP is different technique)

like image 32
tp1 Avatar answered Oct 03 '22 00:10

tp1