I want to handle all of the children of specific class the same way.
So far I have been checking with dynamic_cast
like this:
if(dynamic_cast<ParentClass*>(child_object))
{
// handle the object
}
In the case I do not really need to cast the child object to use it, is there a better way of doing this?
My first attempt was:
if(std::is_base_of<ParentClass, typeid(child_object)>::value)
which of course does not work as the is_base_of
expects two class
arguments and the typeid()
returns the std::type_info
.
So my question is, what is the proper way of doing this? Or is the dynamic_cast
the right facility to use even if the casted object is not used?
Update
Here is the concrete example of what I am trying to achieve. I am iterating over all the QGraphicsItem
objects that are in collision with my object of interest. I want to handle only one group of those objects and ignore the rest. That group of objects has a common parent. So again is using the dynamic_cast
the way to go, or are there better alternatives?
for(QGraphicsItem* i : collidingItems())
{
if(dynamic_cast<ParentClass*>(i))
{
// handle specific group of objects that
//are children of ParentClass
}
}