从超类创建子类?
我正在尝试扩展这个类,
template <class T> class dynamic_array
但这不起作用
class merge_sort : public dynamic_array
扩展该类的正确方法是什么?
I'm trying to extend this class
template <class T> class dynamic_array
This does not work
class merge_sort : public dynamic_array
What is the proper way to extend the class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须为模板提供一个参数。如果您想使用固定参数,例如
int
,那么您会这样做:如果您想将扩展类保留为通用类,您会这样做:
请注意,合并排序是一种算法,并且作为因此,作为一个自由函数比作为一个对象更好。根据 OOP,您应该问merge_sort是
dynamic_array
吗?对我来说,答案听起来是否,所以我会这样做:You have to provide an argument for the template. If you want to use a fixed argument, say
int
, then you would do:If you instead want to keep the extended class as generic, you would do:
Note that merge sort is an algorithm, and as such it would be better off as a free function than as an object. According to OOP, you should ask is
merge_sort
adynamic_array
? For me the answer sounds as no, so I would do this instead:模板参数必须指定给基类,但是您可以驱动形成完全限定的基类。
The template Arguments have to be specified to the base class you can however drive form a fully qualifed base class.