I've got a question, if we have a class A
and other classes from class B : class A
to class Z : class A
are inherited from class A
, and we have a vectorstd::vector<A>
that contains objects of all types from class A
to class Z
, how can we get an object of specific type from that vector, for example we want to get object which is type of E
how to get that object.
class A {};
class B : A {};
class C : A {};
...
class Z : A {};
std::vector<A> vec; // lets say it contains objects of type A to Z
template<typename T>
T GetObject()
{
for (int i = 0; i < vec.size(); i++)
{
//if type of vec[i] == type of T return vec[i]
}
}
E obj = GetObject<E>();
I've used something like this
if (typeid(vec[i]) == typeid(T))
{
return vec[i];
}
but it doesn't work.