C++: Undefined reference to instance in Singleton class

前端 未结 5 1282
醉酒成梦
醉酒成梦 2020-12-20 17:15

I\'m currently trying to implement a factory as a singleton. I practically used the textbook example of the Singleton pattern. Here\'s the .h file:

namespace         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 18:02

    on another side note, maybe you should make the instance pointer a static member of the get function rather than the class, this doesn't make too much difference when using the new/pointer method you are. but if you were just creating a static instance (ie not using a pointer, and returning a reference to it from get get function) this makes a big difference because:

    if its a static member of a class, its constructor is called whenever (because its a global) if its a static member of the get function, it isn't constructed untill it's called the first time, this alleviates some of the problems people have with singletons and them being glorified globals, the other good thing is, most linkers will omit the get function and hence the static instance entirely if it is never called, thus you don't have to worry about calling new so that it only uses memory if it's being used.

提交回复
热议问题