How to implement the factory method pattern in C++ correctly

后端 未结 11 1178
暗喜
暗喜 2020-11-22 14:55

There\'s this one thing in C++ which has been making me feel uncomfortable for quite a long time, because I honestly don\'t know how to do it, even though it sounds simple:<

11条回答
  •  遥遥无期
    2020-11-22 15:14

    extern std::pair const factories[2];
    
    decltype(factories) factories{
      {"blah", []() -> Base*{return new Blah;}},
      {"foo", []() -> Base*{return new Foo;}}
    };
    

提交回复
热议问题