C++ (Qt)template <class T>struct MyClass {... std::vector<T> mData;};
C++ (Qt)typedef MyBaseClass * (*CreateFunc)( int param1... ); MyBaseClass * СreateСlassFloat( int param1... ) { return new MyDerivedClass<float>(param1...); }MyBaseClass * СreateСlassInt( int param1... ) { return new MyDerivedClass<int>(param1...); }... и.т.д
C++ (Qt)template<typename T>MyBaseClass *createClass( int param1... ){ return new MyDerivdClass<T>( param1... );}
C++ (Qt)std::map<std::string, CreateFunc>
C++ (Qt) struct creator{ template<class T> MyBaseClass * operator()( int param1... ) const { return new MyDerivdClass<T>( param1... ); }}; std::list<std::string> type_list;
//Title of this code//g++ 4.9.2#include <functional>#include <iostream>#include <string>#include <map>struct IObject { virtual ~IObject(){} virtual void View()const = 0;};template<class T> struct Object : IObject{ void View()const { std::cout<<typeid(*this).name()<<std::endl; }};template<class T>IObject* CreateObject(){ return new Object<T>; }int main(){ std::cout << "Hello, world!\n"; typedef std::function< IObject*() > CreateFunc; typedef std::map<std::string, CreateFunc> Bind; Bind factory; factory["real"] = CreateObject<float>; factory["integer"] = CreateObject<int>; auto* obj = factory["integer"](); obj->View(); factory["real"]()->View(); }
C++ (Qt)class creator_base{public: virtual ~creator_base() {} virtual MyBaseClass * operator()( int param1... ) const = 0;}; template <class T>class creator : public creator_base{public: virtual MyBaseClass* operator()(int param1...) const { return new MyDerivdClass<T>( param1... ); }}; template <class T>std::shared_ptr<creator<T>> create_function() { return std::make_shared<creator<T>>(); } std::map<std::string, std::shared_ptr<creator_base>> map; map["Real"] = create_function<float>();map["Integer"] =create_function<int>();