Scala 和 C++ 之间有什么关系?特质
Traits 是 Scala 和 C++ 中使用的一个概念(尽管在 C++ 中它更多的是一种习惯用法,而不是集成到语言中的概念)。但对我来说,这些概念之间的关系并不明显。 Scala 和 C++ 特性之间有什么关系?
Traits is a concept used in Scala as well as in C++ (although in C++ it is more of an idiom than a concept integrated into the language). It is not obvious to me how the concepts are related though. What is the relation between Scala and C++ traits?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
他们根本没有关系。
在 C++ 中,特征类是一个辅助对象,它告诉您有关类型的信息,而这些信息是您无法从类型名称本身获得的。 C++ 的特征实际上更类似于 Scala 的 def foo[A:Manifest] 表示法(我不知道该功能的正确名称。)
Scala 的特征实际上更类似于 C++ 的多重继承(尽管它们在细节上有所不同)。事实上,我对 C++ 版本的多重继承有正式名称“多重继承”(排除所有其他变体)感到非常失望,因为对 Scala 特性的任何解释的第一句话应该是是“特征是多重继承的一种形式......”
They're not related at all.
In C++, a traits class is a helper object that tells you something about a type that you can't get from the type name itself. C++ traits are actually more similar to Scala's
def foo[A:Manifest]
notation (a feature for which I don't know the proper name.)Scala's traits are actually a lot more like C++'s multiple inheritance (though they differ in the details). I'm actually quite disappointed that C++'s version of multiple inheritance has the official name "multiple inheritance" (to the exclusion of all of the other variations), because the first sentence of any explanation of Scala's traits should be "Traits are a form of multiple inheritance that ..."