单例初始化

发布于 2024-10-31 15:45:36 字数 487 浏览 1 评论 0原文

我在代码中实现了单例设计模式

假设是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

请别遗忘我 2024-11-07 15:45:36

静态字段除了声明之外还必须有定义。声明通常位于 .h 文件中的类声明中,而定义几乎总是位于 .cpp 文件中。静态变量的定义是必须的,因为它们必须被初始化为某种东西。

但即使定义位于类主体之外,甚至位于完全不同的文件中,也不意味着它不是类的一部分。 SingletonClass:: 使其成为类定义的一部分(而不是类声明),因此它可以“访问”私有字段。

对于类体外部定义的方法也是如此,例如:

// A.h
class A
{
private:
    int b;
public:
    A(int x) : b(x)
    {}

    Do();
}

// A.cpp
A::Do()
{
    return b;
}

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:

// A.h
class A
{
private:
    int b;
public:
    A(int x) : b(x)
    {}

    Do();
}

// A.cpp
A::Do()
{
    return b;
}
星光不落少年眉 2024-11-07 15:45:36

使用单例模式的最佳方法是根本不使用它。

简单总结一下为什么单例是不好的:

  1. 它们是带有特殊限制的全局变量。全局变量由于其自身原因已经够糟糕的了;让它们成为单身只会放大坏处。
  2. 如果您确实只需要一个对象的一个​​实例,那么就创建一个。如果您需要一种特殊的设备来确保您不会制作多个,那么您的代码的语义就有问题。使其成为单例并不能解决问题,它只是用新问题掩盖了它。
  3. 单例与线程不能很好地配合。线程已经够硬的了。不要让他们变得更难。

使用单例并不能解决任何问题。它只是将错误的语义应用于现有代码,使该代码的未来扩展变得困难或不可能(如果明天需要两个怎么办?),并增加新问题。长话短说,只是不要使用它们。

The best way you can use the Singleton pattern is to not use it at all.

A brief summary of why Singletons are bad:

  1. They are globals with special restrictions. Globals are bad enough for their own reasons; making them singletons just amplifies the badness.
  2. If you really need only one instance of an object, then just make one. If you need a special device to ensure you don't make more than one, then there's something wrong with the semantics of your code. Making it a singleton doesn't fix the problem, it just papers over it with new problems.
  3. Singletons don't play nice with threads. Threads are hard enough. Don't make them harder.

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.

感情旳空白 2024-11-07 15:45:36

在初始化代码中,您不是访问 Singleton::s,而是定义它。

In the initialization code, you're not accessing Singleton::s, you're defining it.

半世晨晓 2024-11-07 15:45:36

私有变量可以被类的所有方法访问。您访问 s 变量的唯一位置是在属于同一类的方法 Get() 中。

如果你想从外部访问s,你不能直接这样做,但你必须调用Get()方法(这是公共的)和该方法实际上会为您返回s

用法:

Singleton * s = SingletonClass::Get();

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:

Singleton * s = SingletonClass::Get();
忆梦 2024-11-07 15:45:36

它可以通过 Get 从外部访问(如果您指定了适当的类型)。它是私有这一事实并不能阻止该方法返回指向它的指针。 私有成员只会阻止按名称访问它。

It is accessed from the outside via Get (if you give that the appropriate type). The fact that it is private doesn't prevent that method from returning a pointer to it. A member being private just prevents access to it by name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文