Singleton: How should it be used

后端 未结 24 2270
Happy的楠姐
Happy的楠姐 2020-11-22 04:57

Edit: From another question I provided an answer that has links to a lot of questions/answers about singletons: More info about singletons here:

So I have read th

24条回答
  •  Happy的楠姐
    2020-11-22 05:16

    Modern C++ Design by Alexandrescu has a thread-safe, inheritable generic singleton.

    For my 2p-worth, I think it's important to have defined lifetimes for your singletons (when it's absolutely necessary to use them). I normally don't let the static get() function instantiate anything, and leave set-up and destruction to some dedicated section of the main application. This helps highlight dependencies between singletons - but, as stressed above, it's best to just avoid them if possible.

提交回复
热议问题