In this scenario where classes DeriA and DeriB inherit from Base:
class Base
class DeriA : public Base
class DeriB : public Base
std::list<Base> objects;
Is it possible to check what type of class is being inherited from each member of the objects list? I've tried using static_cast within a try/catch statement in order to check if a certain member of objects is a particular type as seen here:
try
{
DeriA tempA = static_cast<DeriA>(*objects_iterator);
std::cout << "Found A" << std::endl;
} catch(std::bad_cast e)
{
// Dealing with the exception
std::cout << "Found B" << std::endl;
}
However this always outputs "Found A" regardless of whether or not the object that was being accessed is DeriA or DeriB. Could anyone help shed some light as to why?