何时定义虚拟析构函数 [英] When to define a virtual destructor

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

问题描述

Hello people,

我知道在基类中你应该将析构函数定义为虚拟,但是在任何情况下你都将析构函数定义为虚拟。在你实现赋值运算符和复制构造函数的情况下,你也需要实现析构函数,但它是否也需要是虚拟的?


非常感谢

Hello people,
I know that in a base class you should define the destructor as virtual but is there any cases that you define a destructor as virtual. In the case when you implement assignment operator and copy constructor you need to implement the destructor as well but does it need to be virtual too or not?

many thanks

推荐答案


Hello people,

我知道在基类中你应该将析构函数定义为虚拟但是在任何情况下,您将析构函数定义为虚拟。在你实现赋值运算符和复制构造函数的情况下,你需要实现析构函数,但它是否也需要是虚拟的?


非常感谢
Hello people,
I know that in a base class you should define the destructor as virtual but is there any cases that you define a destructor as virtual. In the case when you implement assignment operator and copy constructor you need to implement the destructor as well but does it need to be virtual too or not?

many thanks



我真的不明白你想说什么。


你能用某种方式改写吗?


Savage


I really don''t understand what are you trying to say.

Could you please rephrase somehow?

Savage



我真的不明白你想说什么。


你能用某种方式改写吗?


Savage
I really don''t understand what are you trying to say.

Could you please rephrase somehow?

Savage



我说的基本上就是什么时候做除了继承和基类之外,你还将析构函数定义为虚拟。

well what I''m saying basically is when do you define a destructor as virtual besides in inheritance and for the base class.


哇!析构函数和复制方法!虽然这是一个无辜的问题,但你真的开辟了一个很大的C ++蠕虫。


首先,析构函数,复制构造函数和operator =被称为大 3英寸方法和很多次组合在一起。一些消息来源说,如果你实施其中一个,你应该实现所有这些。在这种情况下,通过将方法设为私有来禁用方法计为实现。


请注意,上面的讨论没有提到析构函数是虚拟的。如果您有任何计划从此类派生,则必须使析构函数为虚拟。如果你不是,你可以引入一个由于拼接引起的错误。这是否意味着基类也必须定义复制方法?仅当析构函数释放资源时。我相信这是Big 3规则中为数不多的例外之一。


运算符=可以虚拟(我想)但是我没有看到这样做的例子。使用operator =的大多数情况都在同一个类之间。我相信operator =可以用于在基类和派生类之间进行复制(但我不打赌它上面的房子)。对我来说,这听起来像一个灾难。


复制的最后一点。确实需要从基类指针复制派生对象。在这种情况下,在创建虚拟克隆方法的地方使用了一种称为克隆的技术。
Whew! Destructors and copy methods! While this is an innocent question, you have really opened up a big C++ can of worms.

First of all, the destructor, copy constructor and operator= are known as the "Big 3" methods and many times are grouped together. Some sources say that if you implement one of them, you should implement all of them. In this case, disabling a method by making it private counts as an implementation.

Notice that the above discussion makes no mention of the destructor being virtual. You must make the destructor virtual, if you have any plans to derive from this class. If you don''t, you can introduce a bug due to "splicing". Does this mean the base class also has to define the copy methods, too? Only if the desctructor is releasing resources. I believe this is one of the few exceptions to the Big 3 rule.

The operator= can be made virtual (I think) but I haven''t seen an example for doing this. Most cases for using operator= is between the same class. I believe operator= can be used to copy between base and derived classes (but I wouldn''t bet the house on it). To me, this sounds like a diaster.

One last point on copying. There is a real need to make copies of derived objects from base class pointers. In this case, a technique called cloning is used where a virtual clone method is created.


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

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