构造函数可以析构函数是虚拟的 [英] Can constructors & destructor be virtual

查看:118
本文介绍了构造函数可以析构函数是虚拟的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构造函数和析构函数可以是虚拟的吗?

还有一个问题

堆栈的写函数push(),当数组已满时,它将数组的长度加倍.

Can constructors & destructor be virtual ?

and one more question

write function push () of stack that it doubles the length of the array when the array is full

推荐答案

要构建对象,构造函数必须是与对象具有相同的类型,因此构造函数不能是虚函数.但是同一件事不适用于析构函数.析构函数可以定义为虚拟的,甚至可以定义为纯虚拟的.如果您曾希望通过指向基类的指针销毁派生类,则可以使用虚拟析构函数.这将确保大多数派生类的析构函数都将被调用.
参考: http://www.cprogramming.com/tutorial/constructor_destructor_ordering.html [
To build an object the constructor must be of the same type as the object and because of this a constructor cannot be a virtual function. But the same thing does not apply to destructors. A destructor can be defined as virtual or even pure virtual. You would use a virtual destructor if you ever expect a derived class to be destroyed through a pointer to the base class. This will ensure that the destructor of the most derived classes will get called.
Ref: http://www.cprogramming.com/tutorial/constructor_destructor_ordering.html[^]

Try the programming part yourself first.


析构函数—是的.这是我对此事的第一个解释: http://www.programmerinterview.com/index.php/c-cplusplus/virtual-destructors/ [ ^ ].

构造函数?..是的,在某些语言中,从不使用C ++ .顺便说一句,静态函数也是一样.虚拟构造函数和静态函数的概念非常强大,对于C ++来说可能太强大了.我很想念.

push的实现我会让您进行一次简单的家庭作业(同样,您也没有明确提出这个小问题).反正还是很有用的运动.

祝你好运,
—SA
Destructor — yes. This is the first explanation of this matter I found: http://www.programmerinterview.com/index.php/c-cplusplus/virtual-destructors/[^].

Constructor?.. Yes, in some languages, never in C++. Same thing about static functions, by the way. The conception of virtual constructors and static functions is extremely powerful, maybe too powerful for C++. I miss it.

The implementation of push I would leave you for a easy home assignment (also, you did not clearly formulate this little problem). Pretty useful exercise, anyway.

Good luck,
—SA


这篇关于构造函数可以析构函数是虚拟的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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