简体   繁体   English

如何解释 c++11 中的 static 代码

[英]how to interpret the static code in c++11

I just found some source codes, written in C++11.我刚刚找到了一些源代码,写在 C++11 中。 The code is like this:代码是这样的:

inline Class& function() {
    static Class x;
    return x;
}

and then for using this function I faced:然后使用这个 function 我面临:

function().app();

Is this function() implements the Singleton Pattern?这个函数()是否实现了 Singleton 模式? How it is working?它是如何工作的? It seems strange to me.我觉得很奇怪。

The subroutine-static means on the stack, and return & means return a pointer to that, all very singular but no guarantee there is no Class x elsewhere in the process. subroutine-static 意味着在堆栈上,return & 意味着返回一个指向它的指针,所有这些都非常奇异,但不能保证在进程的其他地方没有 Class x。 Wiki says you must hide the constructor, have a class method to return a reference to a class-static object: https://en.wikipedia.org/wiki/Singleton_pattern Wiki 说您必须隐藏构造函数,使用 class 方法返回对静态类 object 的引用: https://en.wikipedia.org/wiki/Singleton

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM