class - Create objects from template, type is entered by user input c++ -
i have class template. want program ask user type wants , instantiate object based on type chose. best way this? this, doesn't work:
template <typename t> class object {...}; cin >> type; object<type> newobject;
polymorphism
it make objects template based , dynamic based on user input:
class base { public: virtual ~base() {}; }; template <typename t> class type : public base { t type; }; int main() { int i; cin >> i; base *b; switch (i) { case 0: b = new type<float>(); break; case 1: b = new type<int>(); break; case 2: b = new type<char>(); break; } // ... delete b; }
Comments
Post a Comment