I got to know about run-time type information in c++. This can be accomplished with
typeid keyword in c++.
int main()
{
//these two where user-defined class
Complex x;
Point3D y;
const type_info& s = typeid(x);
cout<<typeid(y).name()<<endl; //works...(1)
cout<<s.name()<<endl; //works...(2)
Complex * tp = new s[10]; //does not work...(3)
}
As marked in the code, I was successful in printing out types of the data objects as in (1) and (2).
Now I wish to allocate memory by using type_info/typeid. Could anyone do this? Is it even possible. I do not have any virtual functions.
Can such a feat be accomplished by any other way. I do not want to use virtual functions as it has a negative effect on code vectorization.
No, this is not possible. What you are looking for is a factory.
Here is an example of such a factory. Suppose you have these types:
You can define the following factory:
And use it like this: