Singleton pattern in C++

前端 未结 8 1838
北荒
北荒 2020-11-30 18:10

I have a question about the singleton pattern.

I saw two cases concerning the static member in the singleton class.

First it is an object, like this

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 18:27

    Neither is more correct than the other. I would tend to try to avoid the use of Singleton in general, but when I've been faced with thinking it was the way to go, I've used both of these and they worked fine.

    One hitch with the pointer option is that it'll leak memory. On the other hand, your first example may end up getting destroyed before you're done with it, so you'll have a battle to wage regardless if you don't choose to figure out a more appropriate owner for this thing, that can create and destroy it at the right times.

提交回复
热议问题