12

Suppose I'm given a C++ library full of inheritance. I'm given a Base* in a function when I know that it is actually pointing to a Derived object and Derived inherits Base. But I don't know what kind of inheritance it is (public/protected/private). I also don't know if there is any virtual function in the hierarchy.

Given this situation, without looking into the source code/documentation of Base and Derived, which cast should I use? Or should I consult the code/documentation first to ensure about polymorphism?

Background

I am writing changeEvent function of QMainWindow in Qt 4.7. The changeEvent function takes QEvent* which I can cast to other type by knowing QEvent::type(). I was wondering if I should use static_cast or dynamic_cast.

Thanks.

Donotalo
  • 12,748
  • 25
  • 83
  • 121
  • 3
    If any library you use uses `protected` inheritance, it is time to find a better written library. – Zac Howland Jan 10 '11 at 15:00
  • This question might help: http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast –  Jan 10 '11 at 15:01
  • 2
    @Zac: Wish I could downvote comments. How can you possibly judge whether or not protected inheritance is the right tool for the job if you don't know what that library does? – Puppy Jan 10 '11 at 15:03
  • @DeadMG: There is absolutely no use for `protected` inheritance. `private` inheritance has its uses (though should be done sparingly), but `protected` is flat out useless. – Zac Howland Jan 10 '11 at 15:08
  • @DeadMG: I'd tend to agree with Zac, `protected` really is a weird accessibility level to begin with -- somewhat useful for some functions (constructors/destructors mostly), it's generally harmful for attributes and I can't begin to fathom what kind of weirdly fashioned inheritance hierarchy would actually *require* protected inheritance. – Matthieu M. Jan 10 '11 at 15:15
  • Just to avoid confusion, I think we are talking apples and oranges here. Since the OP is referring to casting pointers to objects, I'm talking about the following: `class A {}; class B : protected A {};`. – Zac Howland Jan 10 '11 at 15:17

3 Answers3

20

Use static_cast. If you know that your Base* points to a Derived, then use static_cast. dynamic_cast is useful for when it might point to a derived.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 1
    You might know now. But he is using a library and I am sure there is no gurantee provided by the library that they will not change implementation details. The interface is Base* so that is **ALL** you can assume. Therefore you **MUST** use dynamic_cast<> so that you can check that your assumptions hold afterwords. – Martin York Jan 10 '11 at 18:36
  • @Martin: That's his problem, not mine. If he *knows* that it does indeed point to a derived, then the root of that knowledge is out of the bounds of what I can possibly answer for. – Puppy Jan 10 '11 at 18:50
  • 2
    The probelm is he can't know. His knowledge is limited to the time space cordinates of this one particular instance. Thus we need to emphasis that the OP is wrong in his assumption that he knows. He knows now but the knowledge is not transferable to the future. Thus because he is wrong static_cast is the wrong answer (it is the correct answer if the OP was correct(but he is not)). The only time you can assume the interface is Derived is when it returns a Derived* otherwise it may return anything derived from Base* (thats why we have interfaces in the code). – Martin York Jan 10 '11 at 19:13
  • Just for the record, I have used this and you can also use a typeid() check to check the types. – Puppy Apr 14 '15 at 13:41
9

When in doubt, you should prefer dynamic_cast. It might be slower, but you probably won't notice the difference anyway.

If you need speed, use the following snippet:

template <typename Derived, typename Base>
Derived safe_static_cast(Base b)
{
  assert((void*)dynamic_cast<Derived>(b) && "safe_static_cast failed!");
  return static_cast<Derived>(b);
}

Or something equivalent.

The idea is that in debug builds, it checks that it's indeed what you thought it would be, and it release builds... well everything has already been tested, hasn't it ?

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
3

From MSDN -

In general you use static_cast when you want to convert numeric data types such as enums to ints or ints to floats, and you are certain of the data types involved in the conversion. static_cast conversions are not as safe as dynamic_cast conversions, because static_cast does no run-time type check, while dynamic_cast does. A dynamic_cast to an ambiguous pointer will fail, while a static_cast returns as if nothing were wrong; this can be dangerous. Although dynamic_cast conversions are safer, dynamic_cast only works on pointers or references, and the run-time type check is an overhead.

For more info, check out this link

Mahesh
  • 34,573
  • 20
  • 89
  • 115