Looking for a better C++ class factory

前端 未结 10 718
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 00:40

I have an application that has several objects (about 50 so far, but growing). There is only one instance of each of these objects in the app and these instances get shared

10条回答
  •  旧时难觅i
    2020-12-29 01:22

    If you have RTTI enabled, you can get the class name using typeid.

    One question, why are you using a factory rather than using a singleton pattern for each class?


    Edit: OK, so you don't want to be locked into a singleton; no problem. The wonderful thing about C++ is it gives you so much flexibility. You could have a GetSharedInstance() member function that returns a static instance of the class, but leave the constructor public so that you can still create other instances.

提交回复
热议问题