单例初始化
我在代码中实现了单例设计模式。
假设是:
class Singleton
{
Singleton () {}
static Singleton* s;
public:
static Singleton* Get () {
if (!s)
s = new Singleton ();
return s;
}
};
令我困惑的是这个模式的“初始化”。 在 .cpp 中,我输入:
SingletonPointer* SingletonClass::s (0);
但我不明白如何访问定义s
,因为它是私有
。 这怎么可能?
TIA, 吉尔
I implemented the Singleton design pattern in my code.
Suppose it is:
class Singleton
{
Singleton () {}
static Singleton* s;
public:
static Singleton* Get () {
if (!s)
s = new Singleton ();
return s;
}
};
What puzzles me is the 'initialization' of this pattern.
In the .cpp I put:
SingletonPointer* SingletonClass::s (0);
But I don't understand how is it possible to access define s
, as it is private
.
How's that possible?
TIA,
Jir
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
静态字段除了声明之外还必须有定义。声明通常位于 .h 文件中的类声明中,而定义几乎总是位于 .cpp 文件中。静态变量的定义是必须的,因为它们必须被初始化为某种东西。
但即使定义位于类主体之外,甚至位于完全不同的文件中,也不意味着它不是类的一部分。
SingletonClass::
使其成为类定义的一部分(而不是类声明),因此它可以“访问”私有字段。对于类体外部定义的方法也是如此,例如:
Static fields must have definitions besides their declaration. The declaration usually goes in the class declaration in the .h file, while the definition almost always goes in the .cpp file. The definition of static variables is a must, since they must be initialized to something.
But even though the definition is outside of the class body and even in an entirely different file, it doesn't mean it's not a part of the class. The
SingletonClass::
makes it part of the class definition (as opposed to the class declaration), and therefore it can 'access' private fields.The same goes for methods defined outside of the class body, for instance:
使用单例模式的最佳方法是根本不使用它。
简单总结一下为什么单例是不好的:
使用单例并不能解决任何问题。它只是将错误的语义应用于现有代码,使该代码的未来扩展变得困难或不可能(如果明天需要两个怎么办?),并增加新问题。长话短说,只是不要使用它们。
The best way you can use the Singleton pattern is to not use it at all.
A brief summary of why Singletons are bad:
Using a Singleton doesn't solve any problem. It just applies false semantics to existing code, makes future extensions of that code difficult or impossible (what if you need two tomorrow?), and adds new problems. Long story short, just don't use them.
在初始化代码中,您不是访问
Singleton::s
,而是定义它。In the initialization code, you're not accessing
Singleton::s
, you're defining it.私有变量可以被类的所有方法访问。您访问 s 变量的唯一位置是在属于同一类的方法 Get() 中。
如果你想从外部访问s,你不能直接这样做,但你必须调用Get()方法(这是公共的)和该方法实际上会为您返回s。
用法:
The private variables can be accessed by all methods of a class. The only place you are accessing the s variable is in method Get() which belongs to the same class.
If you want to access the s from outside, you can't do it directly, but you have to call the Get() method (which is public) and that method will actually return s for you.
Usage:
它可以通过
Get
从外部访问(如果您指定了适当的类型)。它是私有
这一事实并不能阻止该方法返回指向它的指针。私有
成员只会阻止按名称访问它。It is accessed from the outside via
Get
(if you give that the appropriate type). The fact that it isprivate
doesn't prevent that method from returning a pointer to it. A member beingprivate
just prevents access to it by name.