虚拟析构函数和对象数组 [英] Virtual Destructors and array of objects

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

问题描述

好的,我的问题不是虚拟析构函数和原因,而是更多关于

的意义。

通常我们在基类中有一个虚拟析构函数(和

无意中在派生类中)这样你就可以通过基类指针删除一个

派生类对象......所以,正确的

析构函数被调用(特别是派生类1)和

正确的内存量也被释放。


但是如果以上是的,为什么通过基类指针分配一个派生对象数组然后通过这个基类指针使用delete [] ptr删除它们是一个很好的做法?


我正在浏览关于新物品的常见问题解答以及如何实施整个物品

以便跟踪物品的数量和调用每个相应的析构函数。


因此,如果通过基指针删除派生对象适用于一个对象,
然后我不知道为什么它不能用于通过基指针删除派生的

对象数组。


也许我错过了什么或者也许我的理解不是

完整......任何人都可以对此有所了解。


谢谢。

Ok, my question is not about Virtual destructors and why, but more on
the significance.
Generally we have a virtual destructor in the base class ( and
inadvertently in the derived class) so that you can delete a
derived-class object via a base-class pointer...So, the correct
destructor(s) gets invoked(the derived class one in particular) and the
correct amount of memory is also released.

But if the above is true, why isnt it a good practice to allocate an
array of derived objects via base class pointer and then delete them
via this base class pointer using delete[] ptr ?

I am going through the FAQ about placement new and how the whole thing
is implemented so as to keep track of the number of objects and
therefore call each corresponding destructor.

So if deleting derived objects via base pointer works for one object,
then I dont know why it wouldnt work for deleting an array of derived
objects via base pointer.

Maybe I am missing something or maybe my understanding is not
complete...can anyone throw more light on this.

Thanks.

推荐答案



< am ****** @ gmail.com> skrev i en meddelelse

news:11 ********************* @ g49g2000cwa.googlegro ups.com ...

<am******@gmail.com> skrev i en meddelelse
news:11*********************@g49g2000cwa.googlegro ups.com...
好的,我的问题不是关于虚拟析构函数及其原因,而是更多关于
的意义。
通常我们在基类中有一个虚拟析构函数(并且无意中在派生类中)因此,您可以通过基类指针删除
派生类对象...因此,调用正确的析构函数(特别是派生类)和
也释放了正确的内存量。

但如果上述情况属实,为什么通过基类指针分配一个派生对象数组然后删除是一个好习惯他们通过这个基类指针使用delete [] ptr?

我正在浏览关于放置新的常见问题解答以及如何实现整个事情
以便跟踪对象的数量和
因此调用每个相应的析构函数。

所以如果删除de通过基指针的rived对象适用于一个对象,
然后我不知道为什么它不能通过基指针删除派生的
对象数组。


因为标准是这样说的。

也许我错过了一些东西或者说我的理解不完整......任何人都可以投入更多光线关于这个。

谢谢。
Ok, my question is not about Virtual destructors and why, but more on
the significance.
Generally we have a virtual destructor in the base class ( and
inadvertently in the derived class) so that you can delete a
derived-class object via a base-class pointer...So, the correct
destructor(s) gets invoked(the derived class one in particular) and the
correct amount of memory is also released.

But if the above is true, why isnt it a good practice to allocate an
array of derived objects via base class pointer and then delete them
via this base class pointer using delete[] ptr ?

I am going through the FAQ about placement new and how the whole thing
is implemented so as to keep track of the number of objects and
therefore call each corresponding destructor.

So if deleting derived objects via base pointer works for one object,
then I dont know why it wouldnt work for deleting an array of derived
objects via base pointer.
Because the standard says so.

Maybe I am missing something or maybe my understanding is not
complete...can anyone throw more light on this.

Thanks.




参见线程虚拟析构函数在这个组中。我给了一个

解释为什么它在那里不起作用。


/ Peter



See the thread "virtual destructors" in this group. I have given an
explanation as to why it does not work there.

/Peter


am******@gmail.com 写道:
好的,我的问题不是关于虚拟析构函数和为什么,而是更多关于
的意义。
通常我们在基类中有一个虚拟析构函数(并且无意中在派生类中),这样你就可以删除一个
派生类对象通过基类指针...因此,正确的析构函数被调用(特别是派生类)和
正确的内存量是也发布了。


正确。

但如果上述情况属实,为什么通过基类指针分配一个派生对象数组是一个好习惯然后使用delete [] ptr通过这个基类指针删除它们?


我好像不明白这个问题。一组派生对象

通过基类指针?你能用一些代码来说明吗?你是

是什么意思


base * array = new derived [10];


?一旦你尝试编制索引,那就不会有用了。

我正在浏览关于新布局的常见问题解答以及整个事情是如何实现的,以便跟踪数量对象和
因此调用每个相应的析构函数。

因此,如果通过基指针删除派生对象适用于一个对象,那么我不知道为什么它不能用于删除一个数组通过基指针派生
对象。


删除数组涉及_size_。大小是从数组元素的

类型中获得的...

也许我错过了一些东西或者说我的理解不是完整的......可以任何人都会对此有所了解。
Ok, my question is not about Virtual destructors and why, but more on
the significance.
Generally we have a virtual destructor in the base class ( and
inadvertently in the derived class) so that you can delete a
derived-class object via a base-class pointer...So, the correct
destructor(s) gets invoked(the derived class one in particular) and the
correct amount of memory is also released.
Right.
But if the above is true, why isnt it a good practice to allocate an
array of derived objects via base class pointer and then delete them
via this base class pointer using delete[] ptr ?
I don''t seem to understand the question. An array of derived objects
via base class pointer? Could you illustrate it with some code? Do you
mean

base *array = new derived[10];

? That won''t work as soon as you try indexing.
I am going through the FAQ about placement new and how the whole thing
is implemented so as to keep track of the number of objects and
therefore call each corresponding destructor.

So if deleting derived objects via base pointer works for one object,
then I dont know why it wouldnt work for deleting an array of derived
objects via base pointer.
Deleting an array involves the _size_. The size is obtained from the
type of the array element...
Maybe I am missing something or maybe my understanding is not
complete...can anyone throw more light on this.




我想你需要意识到''删除''和

'之间的区别''删除[] ''。如果您需要帮助,请询问(在您搜索了所有

通常的地方以获得解释之后)。


V



I guess you need to realize the difference between the ''delete'' and
''delete[]''. If you need help, just ask (after you''ve searched all the
usual places for the explanations).

V


2005年6月7日13:41:37 -0700, am ****** @ gmail .com 写道:


[snip]
On 7 Jun 2005 13:41:37 -0700, am******@gmail.com wrote:

[snip]
为什么通过base分配一个派生对象数组是一个好习惯类指针,然后使用delete [] ptr通过这个基类指针删除它们?
why isnt it a good practice to allocate an
array of derived objects via base class pointer and then delete them
via this base class pointer using delete[] ptr ?




你可以分配一个基类指针数组并使用它们

多态(一旦你分配了他们各自的对象和

分配了他们各自指向数组元素的指针,那就是)。

但删除[]只会删除指针本身的存储空间,

不是他们所指向的!


正如其他人所指出的那样(这是或应该是,一个常见问题)一个必须

永远不会分配*对象* polymo数组因为每个

数组元素必须具有相同的大小。


-

Bob Hairgrove
没有********** @ Home.com


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

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