什么是C ++ Mixin-Style? [英] What is C++ Mixin-Style?

查看:239
本文介绍了什么是C ++ Mixin-Style?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

Mixin s是Lisp的概念。来自博士的良好解释Dobbs

Mixins are a concept from Lisp. A good explanation from Dr. Dobbs:


mixin是一个类的一个片段,它的意图是与其他类mixins。

[...]

一个普通的,独立的类(如Person)和一个mixin之间的区别是,mixin模型的一些小功能切片例如,打印或显示),并且不用于独立使用。相反,它应该由一些需要这个功能的其他类组成(例如Person)。

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是允许像多重继承的东西,而没有通常伴随多重继承的所有Bad Things™。

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.

这可以是有点混乱,但是,因为C ++不原生支持mixins;为了在C ++中做mixins,你必须使用多继承!这实际上是什么意思,在实践中,你仍然使用多继承,但你艺术上限制你允许自己使用它。

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.

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

See the article above for an actual mixin implementation.

这篇关于什么是C ++ Mixin-Style?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆