Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 620021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:43:05+00:00 2026-05-13T18:43:05+00:00

I am quite confused with the dynamic_cast keyword in C++. struct A { virtual

  • 0

I am quite confused with the dynamic_cast keyword in C++.

struct A {
    virtual void f() { }
};
struct B : public A { };
struct C { };

void f () {
    A a;
    B b;

    A* ap = &b;
    B* b1 = dynamic_cast<B*> (&a);  // NULL, because 'a' is not a 'B'
    B* b2 = dynamic_cast<B*> (ap);  // 'b'
    C* c = dynamic_cast<C*> (ap);   // NULL.

    A& ar = dynamic_cast<A&> (*ap); // Ok.
    B& br = dynamic_cast<B&> (*ap); // Ok.
    C& cr = dynamic_cast<C&> (*ap); // std::bad_cast
}

the definition says:

The dynamic_cast keyword casts a datum from one pointer or reference
type to another, performing a runtime check to ensure the validity of the cast

Can we write an equivalent of dynamic_cast of C++ in C so that I could better understand things?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:43:06+00:00Added an answer on May 13, 2026 at 6:43 pm

    Here’s a rundown on static_cast<> and dynamic_cast<> specifically as they pertain to pointers. This is just a 101-level rundown, it does not cover all the intricacies.

    static_cast< Type* >(ptr)

    This takes the pointer in ptr and tries to safely cast it to a pointer of type Type*. This cast is done at compile time. It will only perform the cast if the types are related. If the types are not related, you will get a compiler error. For example:

    class B {};
    class D : public B {};
    class X {};
    
    int main()
    {
      D* d = new D;
      B* b = static_cast<B*>(d); // this works
      X* x = static_cast<X*>(d); // ERROR - Won't compile
      return 0;
    }
    

    dynamic_cast< Type* >(ptr)

    This again tries to take the pointer in ptr and safely cast it to a pointer of type Type*. But this cast is executed at runtime, not compile time. Because this is a run-time cast, it is useful especially when combined with polymorphic classes. In fact, in certain cases the classes must be polymorphic in order for the cast to be legal.

    Casts can go in one of two directions: from base to derived (B2D) or from derived to base (D2B). It’s simple enough to see how D2B casts would work at runtime. Either ptr was derived from Type or it wasn’t. In the case of D2B dynamic_cast<>s, the rules are simple. You can try to cast anything to anything else, and if ptr was in fact derived from Type, you’ll get a Type* pointer back from dynamic_cast. Otherwise, you’ll get a NULL pointer.

    But B2D casts are a little more complicated. Consider the following code:

    #include <iostream>
    using namespace std;
    
    class Base
    {
    public:
        virtual void DoIt() = 0;    // pure virtual
        virtual ~Base() {};
    };
    
    class Foo : public Base
    {
    public:
        virtual void DoIt() { cout << "Foo"; }; 
        void FooIt() { cout << "Fooing It..."; }
    };
    
    class Bar : public Base
    {
    public :
        virtual void DoIt() { cout << "Bar"; }
        void BarIt() { cout << "baring It..."; }
    };
    
    Base* CreateRandom()
    {
        if( (rand()%2) == 0 )
            return new Foo;
        else
            return new Bar;
    }
    
    
    int main()
    {
        for( int n = 0; n < 10; ++n )
        {
            Base* base = CreateRandom();
    
                base->DoIt();
    
            Bar* bar = (Bar*)base;
            bar->BarIt();
        }
      return 0;
    }
    

    main() can’t tell what kind of object CreateRandom() will return, so the C-style cast Bar* bar = (Bar*)base; is decidedly not type-safe. How could you fix this? One way would be to add a function like bool AreYouABar() const = 0; to the base class and return true from Bar and false from Foo. But there is another way: use dynamic_cast<>:

    int main()
    {
        for( int n = 0; n < 10; ++n )
        {
            Base* base = CreateRandom();
    
            base->DoIt();
    
            Bar* bar = dynamic_cast<Bar*>(base);
            Foo* foo = dynamic_cast<Foo*>(base);
            if( bar )
                bar->BarIt();
            if( foo )
                foo->FooIt();
        }
      return 0;
    
    }
    

    The casts execute at runtime, and work by querying the object (no need to worry about how for now), asking it if it the type we’re looking for. If it is, dynamic_cast<Type*> returns a pointer; otherwise it returns NULL.

    In order for this base-to-derived casting to work using dynamic_cast<>, Base, Foo and Bar must be what the Standard calls polymorphic types. In order to be a polymorphic type, your class must have at least one virtual function. If your classes are not polymorphic types, the base-to-derived use of dynamic_cast will not compile. Example:

    class Base {};
    class Der : public Base {};
    
    
    int main()
    {
        Base* base = new Der;
        Der* der = dynamic_cast<Der*>(base); // ERROR - Won't compile
    
        return 0;
    }
    

    Adding a virtual function to base, such as a virtual dtor, will make both Base and Der polymorphic types:

    class Base 
    {
    public:
        virtual ~Base(){};
    };
    class Der : public Base {};
    
    
    int main()
    {
        Base* base = new Der;
        Der* der = dynamic_cast<Der*>(base); // OK
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am quite confused. I have a public string() array defined, and each
since I have not been using generics for some time, I'm quite confused at
I'm quite confused by something. I've got 2 select lists, and if you choose
I'm quite confused about the basic concepts of a Hash table. If I were
I'm quite confused about several books in .NET that I have read. Would someone
I am quite confused with how to effectively use the Html.DropDownList helper for ASP.NET
I am quite confused about the difference between an index on table and index
I read Scott Meyers' article on the subject and quite confused about what he
I have recently run across these terms few times but I am quite confused
In URL rewriting, i am quite confused that should i use underscore (_) OR

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.