受保护的vs私人的破坏者 [英] Protected vs Private Destructor

查看:75
本文介绍了受保护的vs私人的破坏者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,受保护的析构函数和私有的析构函数之间是否有区别?如果基类的析构函数是私有的,我想在删除派生的类对象时仍会调用它。

Is there any difference between a protected and a private destructor in C++? If a base classes destructor is private, I imagine that it is still called when deleting the derived class object.

推荐答案

从< a href = http://www.codersource.net/c/c-tutorials/c-tutorial-class.aspx rel = noreferrer>此处:


如果构造函数/析构函数被声明为私有,则无法实例化该类。

If the constructor/destructor is declared as private, then the class cannot be instantiated.

这是正确的,但是可以从类中的另一个方法实例化。同样,如果析构函数为 private ,则该对象也只能从类内部删除。另外,它防止类被继承(或至少阻止所继承的类被实例化/销毁)。

This is true, however it can be instantiated from another method in the class. Similarly, if the destructor is private, then the object can only be deleted from inside the class as well. Also, it prevents the class from being inherited (or at least, prevent the inherited class from being instantiated/destroyed at all).

这篇关于受保护的vs私人的破坏者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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