It combines the type check with the cast, and uses the syntax of the new keyword casts It is only legal for pointers and references to polymorphic types; it will fail to compile for non-polymorphic and incomplete types. For a pointer, a successful cast will result in a pointer correctly cast and adjusted to the applicable part of the object – including multiple inheritance and virtual base class cases – whereas an unsuccessful cast will result in a testable null pointer:
base *b = ...;
derived *d = dynamic_cast
if(d)
... // successful cast
else
... // unsuccessful cast
As dynamic_cast checks that the cast is possible, rather than checking for an exact type match, it is our substitute for an is-kind-of operation.
A dynamic_cast is normally presented as a safe downcast mechanism, but this is a very narrow view of its capabilities: it may also be used for safe crosscasting to sibling classes. In this context the role of dynamic_cast is to query interfaces
Using dynamic_cast to query interface support.
document *d = new wp_document;
...
if(storable *s = dynamic_cast
... // use storable operations for d
if(notifiable *n = dynamic_cast
... // will not be executed for d
1 comment:
Hey..it seems something is missing in article ?
Do u think qns is answered ?
Adding information about QueryInterface would be appreciated ..
Thanks,
Sangam
Post a Comment