什么是 C++ Mixin 风格?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Mixin 是 Lisp 的一个概念。 博士给出了很好的解释。多布斯:
因此,mixin 的要点是允许诸如多重继承之类的东西,而不会出现多重继承通常带来的所有坏事™。
然而,这可能有点令人困惑,因为 C++ 本身并不支持 mixins;为了在 C++ 中“执行”mixin,您必须使用多重继承!这在实践中最终意味着您仍然使用多重继承,但您人为地限制了您允许自己使用它的用途。
请参阅上面的文章了解实际的 mixin 实现。
Mixins are a concept from Lisp. A good explanation from Dr. Dobbs:
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.
如果我没记错的话,在 C++ 中至少有两种创建 mixin 的方法。这来自我见过的一些非常古老的(1995)教程(但它现在几乎完全从互联网上消失了)。
首先,
或者另一种方式使用虚拟继承和兄弟调用:
现在这两个版本都实现了mixins,因为Mixin和Mixin2是独立的类,但它们仍然可以通信。您可以从此类模块创建软件,然后将这些模块绑定到一个大软件。虚拟继承中的 D1 和 D2 之间也发生同样的情况。需要注意的重要一点是,在 mixin 设计中,不同的模块位于同一个 C++ 对象内。 (哦和 CRTP 是不同的技术)
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,
Or another way uses virtual inheritance, and sibling calls:
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)
Mixin 是一个类(或其他代码分组),旨在通过直接包含在另一段代码中来重用。将其视为没有子类型多态性的继承。 CRTP 是一种近似 C++ 中 Mixin 的方法。
A Mixin is a class (or other grouping of code) that is intended to be reused through direct inclusion in another piece of code. Think of it as inheritance without sub-type polymorphism. The CRTP is a way of approximating Mixins in C++.
通常 mixin 被称为小类(通常是模板化的或基于 crtp 的),您可以从中派生“混合”某些功能;通常通过多重继承和基于策略的设计(另请参阅 Alexandrescu 的“现代 C++ 设计”)。
Usually mixins are referred to as small classes (often templated or crtp based) that you derive from to "mix in" some functionality; usually via multiple inheritance and in policy based designs (also see "Modern C++ Design" by Alexandrescu).
C++ 中的 Mixins 使用 奇怪的重复模板模式 (CRTP) 来表达。 这篇文章是他们提供的内容的精彩细分优于其他重用技术...编译时多态性。
Mixins in C++ are expressed using the Curiously Recurring Template Pattern (CRTP). This post is an excellent breakdown of what they provide over other reuse techniques... compile-time polymorphism.