C++ singleton GetInstance() return

后端 未结 4 881
忘掉有多难
忘掉有多难 2021-02-05 12:46

When implementing a singleton in C++, is it better for GetInstance() to return a pointer to the singleton object, or a reference? Does it really matter?

4条回答
  •  情深已故
    2021-02-05 13:31

    It doesn't matter aside from returning a reference implicitly guarantees that the singleton exists. (If it doesn't, you should throw an exception.)

    This also ignores the advice that singletons are evil as much as globals are evil since singletons are basically globals in design pattern clothing.

提交回复
热议问题