0

I just found some source codes, written in C++11. The code is like this:

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

and then for using this function I faced:

function().app();

Is this function() implements the Singleton Pattern? How it is working? It seems strange to me.

TonySalimi
  • 8,257
  • 4
  • 33
  • 62
davidliu
  • 21
  • 4
  • Does this answer your question? [Singleton: How should it be used](https://stackoverflow.com/questions/86582/singleton-how-should-it-be-used) – Mgetz Jun 08 '20 at 13:42
  • [This may also be useful](https://stackoverflow.com/a/1008289/332733) – Mgetz Jun 08 '20 at 13:44

1 Answers1

0

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. 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