Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
116 views
in Technique[技术] by (71.8m points)

What is C++ Mixin-Style?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...