devxlogo

Operator dynamic_cast<> is Aware of Access Specifications

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.

See also  11 Effective Keyword Research Tools and Techniques
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist