Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- I have a base class (Element) which just became an abstract class and breaks registration for my factory. Does anyone see a way for this template to not allocate the abstract class Element, rather, the derived class.
- */
- EFactory::EFactory() { }
- EFactory::~EFactory()
- {
- factoryRegistry.clear();
- }
- EFactory* EFactory::instance()
- {
- static EFactory factory;
- return &factory;
- }
- void EFactory::registerFunction(std::string name,
- std::function<Element* ()> factoryFunction)
- {
- factoryRegistry[name] = factoryFunction;
- }
- std::shared_ptr<Element> EFactory::create(std::string name)
- {
- Element* instance = nullptr;
- auto it = factoryRegistry.find(name);
- if (it != factoryRegistry.end())
- instance = it->second();
- if (instance != nullptr)
- return std::shared_ptr<Element>(instance);
- else
- return nullptr;
- }
- template<typename T>
- class Registrar
- {
- public:
- Registrar(std::string name)
- {
- EFactory::instance()->registerFunction(name,
- [] (void) -> Element* { return new T(); });
- // The error points to the line above
- }
- };
- /* Here's the compile error ...
- triangle.cpp:5:48: required from here registrar.h:18:45: error: invalid new-expression of abstract class type ‘Triangle’
- [] (void) -> Element* { return new T(); });
- ^~~~~~~
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement