Is it possible at runtime to get information about the type of an object and then create an object of this type?
Those. something like:

string type; cin >> type; // ввод типа пользователем SETTYPE(type) obj; // SETTYPE должна установить тип //... использование obj 

    1 answer 1

    You can not do it this way.

    You can, for example, create a variable of the same type as another variable:

     string s; ... decltype(s) str; 

    So, as you wrote - you can play around and get a heir object, depending on the value - something like

     class Base { ... virtual ~Base(){} }; class D1: public Base ... class D2: public Base ... ... Base * makeD(int i) { if (i == 1) return new D1; if (i == 2) return new D2; ... 

    Well, in general, something like that ...

    But in general terms, calculating a type completely at runtime is not that language.

    And where and why do you need it? Is it a question of which hand to hold a nailing microscope? :) In a sense, maybe what you want is solved differently?

    • one
      This question is connected with the attempts to understand the templates :) I had an erroneous idea that the compiler could not create code for the template function in advance. Now it became clear to me: no matter what arguments are passed to the template function, their type will be known at the compilation stage => code generation for a specific instance occurs at the compilation stage - Vitaly
    • Exactly. Then the question of why, is removed :) - Harry