1

Possible Duplicate:
FAQ: Why does dynamic_cast only work if a class has at least 1 virtual method?

I have read that in C++, performing a dynamic cast down the hierarchy of a set of classes, the cast is allowed only in a situation where the classes are polymorphic, such as when the base class is having a virtual function, etc. What is the reason for this limitation? Is it more 'safe' to have a pure virtual function in place of the normal virtual function in the base class?

Thank You!

Community
  • 1
  • 1
Izza
  • 2,389
  • 8
  • 38
  • 60
  • 1
    Shockingly you have to go to the bottom of the answers of that other question (at time of writing) to find one that offers a rationale as well as the quotes from the standard. – Steve Jessop Jan 13 '12 at 13:03
  • 1
    @SteveJessop: It seems that your remark hit the spot, the answer is going up :) – Matthieu M. Jan 13 '12 at 13:12

3 Answers3

3

What is the reason for this limitation?

dynamic_cast is only supposed to succeed when the object is an instance of the target type. Non-polymorphic classes don't contain any type information, so there is no way to tell whether this is the case; therefore, the cast cannot succeed.

Is it more 'safe' to have a pure virtual function in place of the normal virtual function in the base class?

Either is fine, as far as polymorphism is concerned. If the base class has at least one virtual function, then it is polymorphic, and so can be used with dynamic_cast. Whether it's pure or not only affects whether the base class can be instantiated.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

When performing a dynamic_cast there is a type check performed at a runtime and std::bad_cast exception is thrown or null pointer is returned when the cast is illegal. The mechanism allowing to do this is called RTTI.

When class are not polymorphic there is no way to perform that typecheck, because no type information is stored at the runtime.

Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
  • std::bad_cast is only thrown when dynamic_cast'ing a reference. With pointers it returns 0. – noe Jan 13 '12 at 17:48
2

It's a limitation induced by the standard.

In major implementations, dynamic_cast works by comparing the vfptr - virtual function table pointer - of two classes.

That's one way to determine whether classes are related at run-time. Besides, it wouldn't really make sense to do a dynamic_cast on non-polymorphic classes. There's static_cast for that.

My guess is that you're doing something wrong, if you're trying to use dynamic_cast on non-polymorphic classes. If you post some code, we could help.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Thanks, but I just came across this in reading on C++ casting. I couldn't understand the reason why it is limited in such a way, that's why I asked. – Izza Jan 13 '12 at 11:54
  • 3
    @Izza there are different tools to accomplish different things. You shouldn't look at it as a limitation. It's simply a tool to check whether polymorphic classes relate, ergo it works on polymorphic classes. It's like asking why you can't use a bottle opener to open a door... it just wasn't made for that purpose... – Luchian Grigore Jan 13 '12 at 11:57