受保护的构造函数有哪些实际用途?
为什么有人会声明构造函数受保护? 我知道构造函数被声明为私有,目的是不允许它们在堆栈上创建。
Why would anyone declare a constructor protected? I know that constructors are declared private for the purpose of not allowing their creation on stack.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
当一个类是(旨在作为)抽象类时,受保护的构造函数是完全正确的。 在这种情况下,您不希望从类实例化对象,而只想使用它来继承。
还有其他用例,例如某些构造参数集应仅限于派生类。
When a class is (intended as) an abstract class, a protected constructor is exactly right. In that situation you don't want objects to be instantiated from the class but only use it to inherit from.
There are other uses cases, like when a certain set of construction parameters should be limited to derived classes.
当存在无法仅由构造函数保证的构造要求时,非公共构造函数非常有用。 例如,如果需要在构造函数之后立即调用初始化方法,或者如果对象需要向某个容器/管理器对象注册自身,则必须在构造函数之外完成此操作。 通过限制对构造函数的访问并仅提供工厂方法,您可以确保用户收到的任何实例都将满足其所有保证。 这也常用于实现 Singleton,这实际上只是类所做的另一个保证(只有一个实例)。
将构造函数设置为受保护而不是私有的原因与将任何其他方法或字段设置为受保护而不是私有的原因相同:以便它可以由子级继承。 也许您需要基类中的公共非虚拟工厂方法,该方法返回对派生类实例的引用; 派生类显然想要访问父构造函数,但您仍然不希望在工厂之外创建它们。
Non-public constructors are useful when there are construction requirements that cannot be guaranteed solely by the constructor. For instance, if an initialization method needs to be called right after the constructor, or if the object needs to register itself with some container/manager object, this must be done outside the constructor. By limiting access to the constructor and providing only a factory method, you can ensure that any instance a user receives will fulfill all of its guarantees. This is also commonly used to implement a Singleton, which is really just another guarantee the class makes (that there will only be a single instance).
The reason for making the constructor protected, rather than private, is the same as for making any other method or field protected instead of private: so that it can be inherited by children. Perhaps you want a public, non-virtual factory method in the base class, which returns references to instances of the derived classes; the derived classes obviously want access to the parent constructors, but you still don't want to be creating them outside of your factory.
当类的方法都不是纯虚拟的时,受保护的构造函数可用于使类有效地抽象。
从 C++ 意义上来说,它并不是很抽象,因为友元类仍然可以使用它而无需重写,但是您必须声明它们。
A protected constructor can be used to make a class effectively abstract when none of its methods are pure-virtual.
It is not quite abstract in the C++ sense since friend classes can still use it without overriding, but then you would have to declare these.
受保护的构造函数意味着只有派生成员才能使用该构造函数构造类的实例(以及派生实例)。 这听起来有点先有鸡还是先有蛋的问题,但有时在实现类工厂时很有用。
A protected constructor means that only derived members can construct instances of the class (and derived instances) using that constructor. This sounds a bit chicken-and-egg, but is sometimes useful when implementing class factories.
一种用途可能是工厂模式
one use could be factory patterns
对于有副作用的工厂方法。
这将创建该类的实例并保证每个实例都有一个唯一的递增整数 id。 请注意,如果您要使用的构造函数不是默认构造函数,则也必须隐藏默认构造函数。
For factory methods with side-effects.
This creates instances of the class and guarantees that each of them has a unique incrementing integer id. Note that if the constructor you want to use is not the default, you must hide the default too.
让子类使用实例化器不能直接访问的构造函数。
To let a subclass use a constructor that should not be accessible to an instantiator directly.
您可以使用它来限制可以创建它的类,例如:
唯一可以创建它的实例的类是 LevelManager 类,因此您将始终知道 Level 实例是在 LevelManager 中创建的。
You could use it to limit the classes that could create it, for example:
The only class that can create an instance of it is the LevelManager class, so you will always know that the Level instance is created in the LevelManager.
protected 构造函数的一种用途是实现 CRTP 模式,请参见下面的代码:
One use of protected constructor is to implement the CRTP pattern, see the code below: