Currently I have class A
, with derived types, B
and C
.
class A {};
class B : public A {};
class C : public A {};
I then have a vector of type A
, which I append a single object of types B
and C
. Like so:
std::vector<A*> list = new std::vector<A*>;
list->push_back(new B);
list->push_back(new C);
My main question is: How would I go about getting an object of a specific type (that inherits from A), from this vector?
I have attempted to use templates, though this gives unresolved external symbol error
template <class T>
T* GetObject()
{
for (A* object : list)
{
if (typeid(*object) == typeid(T)) return object;
}
throw std::exception("vector does not have object of that type");
}