array of types | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

array of types

can I have a map of types ? I have an array of pointers to a base class every element refers to a son of this base I want to type cast to the son with something like map of array so instead of doing that if(a[i]->gettype()=="type1") ((type1*)a[i])->doit(); else if(a[i]->getttype()=="type2") ((type2)a[i])->doit(); I want to do something like ((type[k])a[i])->doit();

6th Dec 2019, 9:29 PM
ABADA S
ABADA S - avatar
4 Answers
+ 3
good thing but my problem not in declaration type I want to write a list on a file but I don't know what is the type of object I have in the element
6th Dec 2019, 10:05 PM
ABADA S
ABADA S - avatar
+ 2
Try using decltype function. template<typename T> T func(T value) {} dectltype(func(2)) v; Here, dcltype returns the type of the return value of function func. (int in that example)
6th Dec 2019, 9:47 PM
Théophile
Théophile - avatar
+ 2
interested.....
6th Dec 2019, 10:17 PM
Waseem Alhalabi
Waseem Alhalabi - avatar
+ 2
Are all the types stored in your array derived from a common base class? If yes, then you can use polymorphism. Else... I don't know
6th Dec 2019, 10:26 PM
Théophile
Théophile - avatar