C++ 受保护与私有析构函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3246761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Protected vs Private Destructor
提问by doron
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.
C++ 中的受保护和私有析构函数之间有什么区别吗?如果基类析构函数是私有的,我想在删除派生类对象时仍然会调用它。
采纳答案by Amir Rachum
Taken from here:
取自这里:
If the constructor/destructor is declared as private, then the class cannot be instantiated.
如果构造函数/析构函数声明为私有,则无法实例化该类。
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).
这是真的,但是它可以从类中的另一个方法实例化。同样,如果析构函数是private
,则对象也只能从类内部删除。此外,它可以防止类被继承(或者至少防止被继承的类被实例化/销毁)。
回答by Abhay
If the base class destructor is private
or protected
then you cannot call delete
through the base-class pointer.
如果基类析构函数是private
orprotected
那么你不能通过delete
基类指针调用。
Use a protected destructor to prevent the destruction of a derived object via a base-class pointer. It limits access to the destuctor to derived classes. And it prevents automatic (stack) objects of class base.
使用受保护的析构函数来防止通过基类指针破坏派生对象。它将对析构函数的访问限制为派生类。它可以防止类基的自动(堆栈)对象。
In effect it is used to allow any otherpolymorphic use of derived classes via pointers to base, but not allow the users to delete using such a pointer. Example:- Abstract Base Classes / Interfaces.
实际上,它用于允许通过指向基类的指针对派生类进行任何其他多态使用,但不允许用户使用这样的指针进行删除。示例:- 抽象基类/接口。
But a protected
, non-virtual
destructor seems to be a bug waiting to happen. Assuming you do not provide a destroy()
function, you have to eventually make the dtor public. As soon as you do that, you have no further control over the class, and run the risk of polymorphic deletion with a non-virtual dtor, if someone derives further from your class.
但是protected
,non-virtual
析构函数似乎是一个等待发生的错误。假设您不提供destroy()
函数,您最终必须将 dtor 公之于众。一旦你这样做了,你就无法进一步控制这个类,并且如果有人从你的类派生得更远,就会冒着使用非虚拟 dtor 进行多态删除的风险。
回答by Nemanja Trifunovic
The following piece of code will result in the compiler error (VC2010): C2248: 'base::~base' : cannot access private member declared in class 'base'
以下代码将导致编译器错误 (VC2010): C2248: 'base::~base' : 无法访问类 'base' 中声明的私有成员
class base
{
~base(){}
};
class derived : public base
{
};
int main ()
{
derived* d = new derived;
delete d;
}
However, if you change the base destructor to be protected, everything works fine.
但是,如果您将基础析构函数更改为受保护,则一切正常。
回答by Omnifarious
The answer is that your assumption is wrong. The base class destructor cannot be called when it is private.
答案是你的假设是错误的。基类析构函数在私有时无法调用。