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?