C++ 何时使用模板 vs 继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7264402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:42:35  来源:igfitidea点击:

When to use template vs inheritance

c++templatesinheritance

提问by Brent212

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". But say you're in an interview and get asked "When would you use a template instead of inheritance and vice versa?"

我一直在寻找这个,对此的共同反应似乎是“它们无关紧要,一个不能代替另一个”。但是假设你在接受采访时被问到“你什么时候会使用模板而不是继承,反之亦然?”

回答by Kerrek SB

The way I see it is that templates and inheritance are literally orthogonal concepts: Inheritance is "vertical" and goes down, from the abstract to the more and more concrete. A shape, a triange, an equilateral triangle.

我的看法是模板和继承实际上是正交的概念:继承是“垂直的”,并且从抽象到越来越具体。一个形状,一个三角形,一个等边三角形。

Templates on the other hand are "horizontal" and define parallelinstances of code that knowns nothing of each other. Sorting integers is formally the same as sorting doubles and sorting strings, but those are three entirely different functions. They all "look" the same from afar, but they have nothing to do with each other.

另一方面,模板是“水平的”,并且定义了彼此一无所知的代码的并行实例。整数排序与双精度排序和字符串排序在形式上是相同的,但它们是三个完全不同的函数。从远处看,它们都“看起来”一样,但它们之间没有任何关系。

Inheritance provides runtime abstraction. Templates are code generationtools.

继承提供运行时抽象。模板是代码生成工具。

Because the concepts are orthogonal, they may happily be used together to work towards a common goal. My favourite example of this is type erasure, in which the type-erasing container contains a virtual base pointer to an implementation class, but there are arbitrarily many concrete implementations that are generated by a template derived class. Template code generation serves to fill an inheritance hierarchy. Magic.

因为这些概念是正交的,所以它们可以愉快地一起使用以实现共同的目标。我最喜欢的例子是类型擦除,其中类型擦除容器包含一个指向实现类的虚拟基指针,但是有任意多个由模板派生类生成的具体实现。模板代码生成用于填充继承层次结构。魔法。

回答by Gnawme

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

“共同反应”是错误的。在“Effective C++”中,Scott Meyers 在第 41 条中说:

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

第 41 条:理解隐式接口和编译时多态性。

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.
  • 类和模板都支持接口和多态。
  • 对于类,接口是显式的并且以函数签名为中心。多态在运行时通过虚函数发生。
  • 对于模板参数,接口是隐式的并且基于有效的表达式。多态在编译期间通过模板实例化和函数重载解析发生。

回答by justin

use a template in a base (or composition) when you want to retain type safety or would like to avoid virtual dispatch.

当您想要保留类型安全或想要避免虚拟分派时,请在基础(或组合)中使用模板。

回答by cmcginty

Templates are appropriate when defining an interface that works on multiple types of unrelated objects. Templates make perfect sense for container classes where its necessary generalize the objects in the container, yet retain type information.

在定义适用于多种类型的不相关对象的接口时,模板是合适的。模板对于容器类来说非常有意义,因为它必须概括容器中的对象,同时保留类型信息。

In case of inheritance, all parameters must be of the defined parameter type, or extend from it. So when methods operate on object that correctly have a direct hierarchical relationship, inheritance is the best choice.

在继承的情况下,所有参数必须是定义的参数类型,或者从它扩展。因此,当方法对正确具有直接层次关系的对象进行操作时,继承是最佳选择。

When inheritance is incorrectlyapplied, then it requires creating overly complex class hierarchies, of unrelated objects. The complexity of the code will increase for a small gain. If this is the case, then use templates.

当继承被错误地应用时,它需要创建过于复杂的不相关对象的类层次结构。代码的复杂性会增加一点点。如果是这种情况,请使用模板。

回答by Judayle L Dsouza

Template describes an algorithm like decide the result of a comparison between the two objects of a class or sorting them. The type(class) of objects being operated on vary but the operation or logic or step etc is logically the same.

模板描述了一种算法,例如决定类的两个对象之间的比较结果或对它们进行排序。被操作的对象的类型(类)不同,但操作或逻辑或步骤等在逻辑上是相同的。

Inheritance on the other hand is used by the child class only to extend or make more specific the parents functionality. Hope that makes sense

另一方面,子类仅使用继承来扩展或使父类功能更具体。希望这是有道理的