我们什么时候需要 C++ 中的私有构造函数?
我有一个关于 C++ 中的私有构造函数的问题。如果构造函数是私有的,我如何创建该类的实例?
我们应该在类中拥有 getInstance() 方法吗?
I have a question about private constructors in C++. If the constructor is private, how can I create an instance of the class?
Should we have a getInstance() method inside the class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
有一些使用
private
构造函数的场景:限制除
friend
之外的所有人的对象创建;在这种情况下,所有构造函数都必须是私有
<前><代码>A类
{
私人的:
一个 () {}
民众:
// 其他可访问的方法
朋友B级;
};
B级
{
民众:
A* Create_A () { 返回新的 A; } // 仅具有“B”的创建权限
};
限制某些类型的构造函数(即复制构造函数、默认构造函数)。例如
std::fstream
不允许通过这种不可访问的构造函数进行复制<前><代码>A类
{
民众:
一个();
A(整数);
私人的:
A(const A&); // C++03: 即使是“朋友”也不能使用这个
A(const A&) = 删除; // C++11:设置 `private` 并不重要
};
要拥有一个公共的委托构造函数,该构造函数不应暴露给外部世界:
<前><代码>A类
{
私人的:
整数x_;
A (const int x) : x_(x) {} // 公共委托;但在“A”的范围内
民众:
A (const B& b) : A(b.x_) {}
A (const C& c) : A(c.foo()) {}
};
For 单例模式,当单例
类
不可继承时(如果可继承,则使用受保护的
构造函数)There are a few scenarios for having
private
constructors:Restricting object creation for all but
friend
s; in this case all constructors have to beprivate
Restricting certain type of constructor (i.e. copy constructor, default constructor). e.g.
std::fstream
doesn't allow copying by such inaccessible constructorTo have a common delegate constructor, which is not supposed to be exposed to the outer world:
For singleton patterns when the singleton
class
is not inheritible (if it's inheritible then use aprotected
constructor)私有构造函数通常与Builder方法一起使用,例如在命名构造函数习惯用法中。
在这个(典型)示例中,命名构造函数习惯用法用于明确使用哪个坐标系来构建
Point
对象。A private constructor is commonly used with Builder methods, for example in the Named Constructor idiom.
In this (typical) example, the Named Constructor idiom is used to make it explicitly which coordinate system is used to build the
Point
object.当您想要控制类的对象创建时,私有构造函数非常有用。
让我们尝试一下代码:
aTestClass a 行会导致错误,因为该行间接尝试访问私有构造函数。注释掉这一行并运行程序。它运行得非常好。现在的问题是如何在这种情况下创建对象。我们来编写另一个程序。
输出是
我们创建了一个包含私有构造函数的类的对象。
使用这个概念来实现单例类
A private constructor is useful when you want to control the object creation of a class.
Let’s try in code:
The line aTestClass a causes an error because this line is indirectly trying to access the private constructor. Comment out this line and run the program. It runs absolutely fine. Now the question is how to create the object in a such case. Let's write another program.
The output is
so we have created an object of the class containing a private constructor.
Use this concept to implement a singleton class
是的,这通常用在通过静态成员函数访问对象的单例模式中。
Yes, this is commonly used in the Singleton pattern where the object is accessed through a static member function.
如果某个构造函数是私有的,则意味着除了类本身(和朋友)之外,没有人能够使用该构造函数创建它的实例。因此,您可以提供静态方法(如 getInstance())来创建类的实例或在某些友元类/方法中创建实例。
If some constructor is private, it means that no one but the class itself (and friends) should be able to create instances of it using that constructor. Therefore, you can provide static methods like getInstance() to create instances of the class or create the instances in some friend class/method.
这取决于为什么构造函数首先被设为私有(您应该询问编写您正在编辑的类的人)。有时,可以将构造函数设为私有以禁止复制构造(同时允许通过其他构造函数进行构造)。其他时候,构造函数可能会被设为私有,以禁止创建该类,除非该类的“朋友”除外(如果该类是“帮助器”,则通常会这样做,该类只能由该帮助器类所在的类使用被创建)。构造函数也可以设为私有,以强制使用(通常是静态的)创建函数。
It depends on why the constructor was made private in the first place (you should ask whoever wrote the class you are editing). Sometimes a constructor may be made private to disallow copy construction (while allowing construction through some other constructor). Other times a constructor may be made private to disallow creating the class except by the class's "friend"s (this is commonly done if the class is a "helper" that should only be used by the class(es) for which the helper class was created). A constructor may also be made private to force the use of a (usually static) creation function.
如果创建私有构造函数,则需要在类内创建对象
If you create a private constructor you need to create the object inside the class
C++ 中的私有构造函数可用于限制常量结构的对象创建。您可以在相同范围内定义类似的常量,如枚举:
像
MathConst::ANG_180
一样访问它。A private constructor in C++ can be used for restricting object creation of a constant structure. And you can define a similar constant in the same scope like enum:
Access it like
MathConst::ANG_180
.我将它与返回 std::Optional 的
friend
函数结合使用,以便安全地构造对象,同时避免异常。例如:并像这样使用它:
I use it in combination with
friend
function(s) returningstd::optional
in order to safely construct objects while avoiding exceptions. For example:And using it like: