Operator dynamic_cast<> is Aware of Access Specifications

Operator dynamic_cast<> fails when it cannot convert its argument to the desired type. Usually, such a failure results from an attempt to cast an object to a non-related class. But there can be another reason for it–an attempt to cast a derived object to its base class will fail in case of private inheritance:

 class Container {/*..*/ };class Allocator{/*..*/};class Stack: public Container, private Allocator  {/*..*/ }; void main(){  Stack s; Allocator* p = dynamic_cast (&s); // runtime failure; Allocator is an inaccessible base}

In this example, dynamic_cast fails because Stack is not publicly derived from Allocator.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: