派生对象的模板是基类型模板的子类吗 [英] Is Template of derived object a subclass of template of base type

查看:25
本文介绍了派生对象的模板是基类型模板的子类吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 'apple' 是 'fruit' 的子类,那么 ListList 的子类,这样对吗?

If 'apple' is a subclass of 'fruit' , then List<apple> is a subclass of List<fruit> is this correct ?

推荐答案

没有.ListList 不相关.这两个类可能有完全不同的实现,并且无法将一个转换为另一个.例如,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 apples 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆