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

  • Home
  • SEARCH
  • 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 894113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:19:46+00:00 2026-05-15T14:19:46+00:00

This have been asked in the interview. How to write own dynamic_cast. I think,

  • 0

This have been asked in the interview.

How to write own dynamic_cast. I think, on the basis of typeid’s name function.

Now how to implement own typid? I have no clue on it.

  • 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-15T14:19:47+00:00Added an answer on May 15, 2026 at 2:19 pm

    There is a reason you don’t have any clue, dynamic_cast and static_cast are not like const_cast or reinterpret_cast, they actually perform pointer arithmetic and are somewhat typesafe.

    The pointer arithmetic

    In order to illustrate this, think of the following design:

    struct Base1 { virtual ~Base1(); char a; };
    struct Base2 { virtual ~Base2(); char b; };
    
    struct Derived: Base1, Base2 {};
    

    An instance of Derived should look something like this (it’s based on gcc since it is actually compiler dependent…):

    |      Cell 1       | Cell 2 |      Cell 3        | Cell 4 |
    | vtable pointer    |    a   | vtable pointer     |    b   |
    |         Base 1             |        Base 2               |
    |                     Derived                              |
    

    Now think of the work necessary for casting:

    • casting from Derived to Base1 does not require any extra work, they are at the same physical address
    • casting from Derived to Base2 necessitates to shift the pointer by 2 bytes

    Therefore, it is necessary to know the memory layout of the objects to be able to cast between one derived object and one of its base. And this is only known to the compiler, the information is not accessible through any API, it’s not standardised or anything else.

    In code, this would translate like:

    Derived derived;
    Base2* b2 = reinterpret_cast<Base2>(((char*)&derived) + 2);
    

    And that is, of course, for a static_cast.

    Now, if you were able to use static_cast in the implementation of dynamic_cast, then you could leverage the compiler and let it handle the pointer arithmetic for you… but you’re still not out of the wood.

    Writing dynamic_cast ?

    First things first, we need to clarify the specifications of dynamic_cast:

    • dynamic_cast<Derived*>(&base); returns null if base is not an instance of Derived.
    • dynamic_cast<Derived&>(base); throws std::bad_cast in this case.
    • dynamic_cast<void*>(base); returns the address of the most derived class
    • dynamic_cast respect the access specifications (public, protected and private inheritance)

    I don’t know about you, but I think it’s going to be ugly. Using typeid is not sufficient here:

    struct Base { virtual ~Base(); };
    struct Intermediate: Base {};
    struct Derived: Base {};
    
    void func()
    {
      Derived derived;
      Base& base = derived;
      Intermediate& inter = dynamic_cast<Intermediate&>(base); // arg
    }
    

    The issue here is that typeid(base) == typeid(Derived) != typeid(Intermediate), so you can’t rely on that either.

    Another amusing thing:

    struct Base { virtual ~Base(); };
    struct Derived: virtual Base {};
    
    void func(Base& base)
    {
      Derived& derived = static_cast<Derived&>(base); // Fails
    }
    

    static_cast doesn’t work when virtual inheritance is involved… so we’ve go a problem of pointer arithmetic computation creeping in.

    An almost solution

    class Object
    {
    public:
      Object(): mMostDerived(0) {}
      virtual ~Object() {}
    
      void* GetMostDerived() const { return mMostDerived; }
    
      template <class T>
      T* dynamiccast()
      {
        Object const& me = *this;
        return const_cast<T*>(me.dynamiccast());
      }
    
      template <class T>
      T const* dynamiccast() const
      {
        char const* name = typeid(T).name();
        derived_t::const_iterator it = mDeriveds.find(name);
        if (it == mDeriveds.end()) { return 0; }
        else { return reinterpret_cast<T const*>(it->second); }
      }
    
    protected:
      template <class T>
      void add(T* t)
      {
        void* address = t;
        mDerived[typeid(t).name()] = address;
        if (mMostDerived == 0 || mMostDerived > address) { mMostDerived= address; }
      }
    
    private:
      typedef std::map < char const*, void* > derived_t;
      void* mMostDerived;
      derived_t mDeriveds;
    };
    
    // Purposely no doing anything to help swapping...
    
    template <class T>
    T* dynamiccast(Object* o) { return o ? o->dynamiccast<T>() : 0; }
    
    template <class T>
    T const* dynamiccast(Object const* o) { return o ? o->dynamiccast<T>() : 0; }
    
    template <class T>
    T& dynamiccast(Object& o)
    {
      if (T* t = o.dynamiccast<T>()) { return t; }
      else { throw std::bad_cast(); }
    }
    
    template <class T>
    T const& dynamiccast(Object const& o)
    {
      if (T const* t = o.dynamiccast<T>()) { return t; }
      else { throw std::bad_cast(); }
    }
    

    You need some little things in the constructor:

    class Base: public Object
    {
    public:
      Base() { this->add(this); }
    };
    

    So, let’s check:

    • classic uses: okay
    • virtual inheritance ? it should work… but not tested
    • respecting access specifiers… ARG :/

    Good luck to anyone trying to implement this outside of the compiler, really 😡

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.