2

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
        }
    }
dsalaj
  • 2,857
  • 4
  • 34
  • 43
  • 3
    Ordinarily you don't need to do *anything* to access base class functionality. What's the concrete problem at hand? Do add a **complete but minimal** example that readers can compile and try out. – Cheers and hth. - Alf Aug 13 '15 at 11:32
  • 1
    Why not have an abstract virtual function in the base class, that is implemented/overridden in the child classes, and you just call that? – Some programmer dude Aug 13 '15 at 11:32
  • You're looking for `decltype`, not `typeid`. But indeed, what are you trying to do ? – Quentin Aug 13 '15 at 11:35
  • @Quentin `decltype` is evaluated during compilation not execution. – Glapa Aug 13 '15 at 11:45
  • @Glapa It's a template argument, so there's no going around providing it at compile-time. Classes can't change bases at runtime anyway, so it's all pointless. – Quentin Aug 13 '15 at 11:47
  • @Quentin Ah sorry I didn't noticed it at first, my bad. – Glapa Aug 13 '15 at 11:53

1 Answers1

5

dynamic_cast is the way to go. It is the only way to detect if the object is part of the inheritance tree for a certain class, since typeid will only give you the actual name of the class.

That being said, if something specific needs to be done on objects of a given class, it should be a virtual method. Using RTTI is bad form usually, but especially here.

slaphappy
  • 74
  • 4
  • This answer fits a question about downcasting, but this one is about upcasting. – Quentin Aug 13 '15 at 13:18
  • @slaphappy Could you please elaborate on your last sentence. I have read about how expensive RTTI is [here](http://stackoverflow.com/questions/579887/how-expensive-is-rtti) (I like the answer from user Marius). But what do you mean by it is especially bad form here? From what I see, I do not have an alternative to the `dynamic_cast` in my case. – dsalaj Aug 13 '15 at 22:14