什么是 C++ Mixin 风格?

发布于 2024-11-29 15:57:49 字数 311 浏览 2 评论 0原文

我刚刚遇到这个关键字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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

§普罗旺斯的薰衣草 2024-12-06 15:57:49

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

mixin 是类的片段,因为它旨在与其他类或 mixin 组合在一起。
[...]
常规的独立类(例如 Person)和 mixin 之间的区别在于,mixin 模拟一些小的功能片(例如,打印或显示),并且不适合独立使用。相反,它应该与需要此功能的其他一些类(例如,Person)组成。

因此,mixin 的要点是允许诸如多重继承之类的东西,而不会出现多重继承通常带来的所有坏事™。

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

请参阅上面的文章了解实际的 mixin 实现。

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.

杀お生予夺 2024-12-06 15:57:49

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

首先,

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();
}

或者另一种方式使用虚拟继承和兄弟调用:

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();
}

现在这两个版本都实现了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,

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)

挖个坑埋了你 2024-12-06 15:57:49

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

才能让你更想念 2024-12-06 15:57:49

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

兮颜 2024-12-06 15:57:49

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文