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

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -