什么是 C++ Mixin 风格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7085265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:27:19  来源:igfitidea点击:

What is C++ Mixin-Style?

c++mixins

提问by Bitmap

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

我刚刚遇到这个关键字C++ Mixin-Style,有人知道这是什么吗?

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

这篇文章中,is 已被回答为一种设计模式。它是否与本文档中描述的设计模式相同?

回答by BlueRaja - Danny Pflughoeft

Mixinsare a concept from Lisp. A good explanation from Dr. Dobbs:

Mixin是 Lisp 的一个概念。多布斯博士的一个很好的解释:

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).

从某种意义上说,mixin 是一个类的片段,它旨在与其他类或 mixin 组合。
[...]
常规的、独立的类(例如 Person)和 mixin 之间的区别在于,mixin 对一些小功能切片(例如,打印或显示)进行建模,并且不适合独立使用。相反,它应该与其他需要此功能的类(例如,Person)组合在一起。

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

所以,mixin 的重点是允许多重继承这样的东西,而没有所有的坏事?这通常伴随着多重继承。

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.

然而,这可能有点令人困惑,因为 C++ 本身并不支持 mixin。为了在 C++ 中“做”mixin,你必须使用多重继承!这在实践中最终意味着您仍然使用多重继承,但您人为地限制了您允许自己使用它的目的。

See the article above for an actual mixin implementation.

有关实际的 mixin 实现,请参阅上面的文章。

回答by tp1

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).

如果我没记错的话,至少有两种方法可以在 C++ 中创建 mixin。这来自我看过的一些非常古老的(1995)教程(但它现在几乎完全从互联网上消失了)。

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)

现在这两个版本都实现了mixin,因为Mixin和Mixin2是独立的类,但是它们仍然可以通信。你可以从这种模块创建软件,然后将这些模块绑定到一个大软件。在虚拟继承中 D1 和 D2 之间也会发生同样的情况。需要注意的重要一点是,在 mixin 设计中,不同的模块位于同一个 C++ 对象中。(哦和 CRTP 是不同的技术)

回答by Mankarse

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 是一种在 C++ 中逼近 Mixin 的方法。

回答by Jesse Pepper

Mixins in C++ are expressed using the Curiously Recurring Template Pattern(CRTP). This postis an excellent breakdown of what they provide over other reuse techniques... compile-time polymorphism.

C++ 中的 Mixin 使用Curiously Recurring Template Pattern(CRTP) 表示。 这篇文章很好地分解了他们提供的其他重用技术......编译时多态性。

回答by PlasmaHH

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).

通常 mixin 被称为小类(通常是模板化的或基于 crtp 的),您可以从中派生出“混合”某些功能;通常通过多重继承和基于策略的设计(另请参阅 Alexandrescu 的“现代 C++ 设计”)。