派生对象的模板是基类型模板的子类吗 [英] Is Template of derived object a subclass of template of base type
问题描述
如果 'apple' 是 'fruit' 的子类,那么 List
是 List
的子类,这样对吗?
If 'apple' is a subclass of 'fruit' , then List<apple>
is a subclass of List<fruit>
is this correct ?
推荐答案
没有.List
和 List
不相关.这两个类可能有完全不同的实现,并且无法将一个转换为另一个.例如,List<>
模板可以以某种方式专门用于 apple
与一般 List
具有完全不同的结构> 模板.
No. List<apple>
and List<fruit>
are not related. These both classes could have totally different implementations and there is no way to convert one into the other. For example the List<>
template could be specialized in some way for apple
s that has a totally different structure than the general List<T>
template.
即使实现是兼容的,将 List
视为 List
仍然是一个坏主意,因为人们会开始把所有种类的水果都放在应该包含苹果的列表中.另请参阅C++ FAQ Lite 中的条目,说到苹果和香蕉...
And even if the implementations were compatible, it would still be a bad idea to treat a List<apple>
as a List<fruit>
since then people would start to put all kinds of fruits into that list that was just supposed to contain apples. See also this entry in the C++ FAQ Lite, that talks about apples and bananas...
这篇关于派生对象的模板是基类型模板的子类吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!