简体   繁体   English

删除构造函数是继承的吗?

[英]Is the deletion of a constructor inherited?

Using the keyword delete you can prevent the compiler from automatically adding standard implementations of certain constructors. 使用关键字delete可以防止编译器自动添加某些构造函数的标准实现。

Is this deletion inherited to subclasses? 这个删除是继承到子类吗?

Apparently, but I wouldn't exactly say that the attribute is inheirited. 显然,但我不会确切地说该属性是不健康的。 It is due to the fact that the compiler generated derived class constructor uses the base class constructor. 这是因为编译器生成的派生类构造函数使用了基类构造函数。 For example, the compiler generated default constructor of a derived class uses the default constructor of the base class. 例如,编译器生成的派生类的默认构造函数使用基类的默认构造函数。 So if the base class default constructor does not exist, for whatever reason (whether it was explicitly deleted, or some other reason), the compiler cannot generate a default constructor for the derived class. 因此,如果基类默认构造函数不存在,无论出于何种原因(无论是显式删除还是其他原因),编译器都无法为派生类生成默认构造函数。 But this doesn't stop you from creating your own constructor for the derived class which uses a different base class constructor than the one which was deleted. 但这并不能阻止您为派生类创建自己的构造函数,该派生类使用与删除的基类构造函数不同的基类构造函数。

Using the keyword delete you can prevent the compiler from automatically adding standard implementations of certain constructors. 使用关键字delete可以防止编译器自动添加某些构造函数的标准实现。

Well, not really. 好吧,不是真的。 Yes, it does accomplish that purpose. 是的,它确实实现了这个目的。 But it also prevents you from implementing that constructor yourself. 但它也会阻止自己实现该构造函数。

Constructors are not inherited, in general. 一般而言,构造函数不是继承的。 Deleting a constructor in the base class does not cause the corresponding constructor in the derived class to be deleted too. 删除基类中的构造函数不会导致派生类中的相应构造函数也被删除。 It does force the compiler to implicitly define the corresponding constructor in the derived class as deleted---but only if you don't define it yourself. 确实强制编译器隐式地将派生类中的相应构造函数定义为已删除---但仅当您没有自己定义它时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM