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 8840601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:30:01+00:00 2026-06-14T10:30:01+00:00

In what scenarios can be reinterpret_cast used to cast from a base pointer that’s

  • 0

In what scenarios can be reinterpret_cast used to cast from a base pointer that’s actually a derived instance pointer? (via polymorphism).

Static casts do not work if the inheritance is polymorphic.

I considered this trivial scenario:

class A
{
public:
    virtual void Hello()
    {
        cout<<" A ";
    }
    virtual int GetType() { return 1; }
};

class B: public A
{
public:
    void Hello()
    {
        cout<< " B ";
    }
    void Do()
    {
        cout << " Another method of B";
    }
    int GetType() { return 2;}
};

/// ... sample/test code
A* a1 = new A();
A* b1 = new B();
A* a2;
B* b2;

if (a1->GetType() == 1)
{
    a2 = a1;
    a2->Hello();
}
else
if (a1->GetType() == 2)
{
    b2 = reinterpret_cast<B*>(a1);
    b2->Do();
    b2->Hello();
}

Mind the very naive “pseudo type identification method GetType() ) I used to decide whether I can convert them or not. Is it downright wrong to use reinterpret_casts at all, for such purposes, of avoid dynamic_casts? (i.e. is it a paranoic design, inherently dangerous and less flexible that can introduce unwanted trouble? Might it be safer and worth the minor performance cost to perform normal dynamic casts? I know that multiple inheritance and/or virtual inheritance will mess up any other cast operation, except for the polymorphic/dynamic one).

  • 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-06-14T10:30:02+00:00Added an answer on June 14, 2026 at 10:30 am

    You can’t use reinterpret_cast to downcast safely. But you can use

    • static_cast, when you know that the dynamic type of the object is (possibly derived from) the one you cast down to, and

    • dynamic_cast, of reference or pointer, if the statically known class is polymorphic.

    In the other direction, for an upcast you can (but should not) use a C style cast in order to cast to an inaccessible base. It’s specially supported in the standard. I have never found occasion to use it, though.

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

Sidebar

Related Questions

I'm looking for a set of scenarios that can be used to assess the
If two scenarios can occur at the same time, does that (always/ever) constitute a
Although obviously not all scenarios can be covered by a single design, is it
How can I implement these scenarios using forms authentication? If the user is Inactive
Can anyone enlighten me in what scenarios I would get the following error in
Can anyone come up with guidelines suggesting the ideal scenarios to choose mocking versus
Okay, I just can't get my head around multi-threading scenarios properly. Sorry for asking
How would I create the following scenario that rails can provide for carrierwave, using
Can I get a complete simple scenario i.e. tutorial that suggest how this should
Can an Android process ever host multiple Dalvik VM's? If so, what scenarios can

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.