且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

调用堆栈上的Singleton类

更新时间:2023-11-13 10:54:10

单例的全部意义不在于何时何地被初始化。第一次访问将初始化该对象,而所有后续访问将产生相同的对象。

The whole point of singleton is not to rely on when and where it is initialized. The first access would initialize the object, and all subsequent access would yield the same object.

如果将对象放在堆栈上,则在堆栈被破坏时,它将被破坏。关闭(退出范围),因此将来的访问将产生无效对象或不同的对象,但不一样(因为您不在同一个范围之内)。

If you put the object on the stack - it will be destructed when the stack is closed (you exit out of the scope), thus future accesses would yield either invalid or a different object, but not the same (as you're out of the scope for the same one).

因此,根据单例的定义,它无法完成。

Thus, by definition of singleton, it cannot be done.

现在,如果您解释要解决的问题,则有人可以提供帮助您可以通过更明智的方式解决它。

Now, if you explain the problem you're trying to solve, someone may help you with solving it in some more sensible way.