【问题标题】:why mesos code prefer template than heritage?为什么 mesos 代码更喜欢模板而不是遗产?
【发布时间】:2017-03-06 10:57:00
【问题描述】:

https://github.com/apache/mesos/blob/master/include/mesos/module.hpp#L56

mesos 模块都派生自 struct ModuleBase,并且在创建不同的模块实例时使用模板。 我很好奇,为什么他们更喜欢模板而不是遗产?是因为运行时间速度的原因吗?

我也注意到,很多 C++ 开源项目都喜欢使用大量模板,导致代码难以阅读和理解,为什么?

【问题讨论】:

    标签: c++ open-source mesos mesosphere


    【解决方案1】:

    似乎有一些一般性的讨论可能对稍后在这里的人有所帮助:

    When should I use templates instead of inheritance, and vice versa?

    When to use template vs inheritance

    http://www.gotw.ca/publications/mill06.htm

    http://people.cs.uchicago.edu/~jacobm/pubs/templates.html

    模板比继承更快,因为它选择在编译时而不是在运行时调用正确的函数。

    它们也有非常相似的功能,它们都支持接口和多态。

    【讨论】:

      猜你喜欢
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      相关资源
      最近更新 更多