dynamic cast - Real world example of dynamic_cast in C++ -
can give me real world example of case when dynamic_cast needed , can't worked around @ all? examples can think of can worked around double dispatch.
if constraint strong, example dynamic_cast way go nice.
i'd see real examples instead of "it's used cast between types , down type tree".
double dispatch requires types interacting have intimate knowledge of each other's innards, requires 1 class call methods on other class. dynamic_cast
works when cannot modify innards of class, or not wish break encapsulation of classes in question.
that is, double dispatch invasive on classes involved, while dynamic_cast
works without knowledge of cast in classes.
you can use dynamic_cast
if don't know target method overload invoked. example, see this question posted yesterday.
finally, double dispatch not come without it's own headaches
the base class shape must know derived classes, resulting in circular dependencies. if derive new class shape (say triangle), must update interface of shape , interface/implementation of other derived classes. in cases not option: may not have source code shape, or not willing or permitted modify it.
Comments
Post a Comment