Files
cpp-templates/typefactory/main.cpp

43 lines
848 B
C++

#include <iostream>
#include "typefactory.h"
#include <string>
using namespace std;
int main() {
cout << "Hello World!" << endl;
struct Base {
virtual ~Base() = default;
virtual int get() = 0;
};
struct Derived1 : Base {
explicit Derived1 (int start) : m_start (start) {}
int get() override {
return m_start + 1;
}
int m_start = 0;
};
struct Derived2 : Base {
explicit Derived2 (int start) : m_start (start) {}
int get() override {
return m_start + 2;
}
int m_start = 0;
};
TypeFactory<std::string, Base, int> factory;
factory.registerType<Derived1> ("one");
factory.registerType<Derived2> ("two");
auto a = factory.create ("one", 10);
auto b = factory.creator ("two") (10); // advanced API: get creator function
(void)a->get();
(void)b->get();
return 0;
}