When to use template vs inheritance

后端 未结 5 1040
自闭症患者
自闭症患者 2020-12-07 12:57

I\'ve been looking around for this one, and the common response to this seems to be along the lines of \"they are unrelated, and one can\'t be substituted for the other\". B

5条回答
  •  遥遥无期
    2020-12-07 13:11

    The "common response" is wrong. In "Effective C++," Scott Meyers says in Item 41:

    Item 41: Understand implicit interfaces and compile-time polymorphism.

    Meyers goes on to summarize:

    • Both classes and templates support interfaces and polymorphism.
    • For classes, interfaces are explicit and centered on function signatures. Polymorphism occurs at runtime through virtual functions.
    • For template parameters, interfaces are implicit and based on valid expressions. Polymorphism occurs during compilation through template instantiation and function overloading resolution.

提交回复
热议问题