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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:10:38+00:00 2026-05-13T21:10:38+00:00

AFAIK, for pointers/references static_cast, if a class definition is not visible to compiler at

  • 0

AFAIK, for pointers/references static_cast, if a class definition is not visible to compiler at this point, then static_cast will be behave like reinterpret_cast.

Why is static_cast unsafe for pointers/references and is safe for numeric values?

  • 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-13T21:10:39+00:00Added an answer on May 13, 2026 at 9:10 pm

    In short, because of multiple inheritance.

    In long:

    #include <iostream>
    
    struct A { int a; };
    struct B { int b; };
    struct C : A, B { int c; };
    
    int main() {
        C c;
        std::cout << "C is at : " << (void*)(&c) << "\n";
        std::cout << "B is at : " << (void*)static_cast<B*>(&c) << "\n";
        std::cout << "A is at : " << (void*)static_cast<A*>(&c) << "\n";
    
    }
    

    Output:

    C is at : 0x22ccd0
    B is at : 0x22ccd4
    A is at : 0x22ccd0
    

    Note that in order to convert correctly to B*, static_cast has to change the pointer value. If the compiler didn’t have the class definition for C, then it wouldn’t know that B was a base class, and it certainly wouldn’t know what offset to apply.

    But in that situation where no definition is visible, static_cast doesn’t behave like reinterpret_cast, it’s forbidden:

    struct D;
    struct E;
    
    int main() {
        E *p1 = 0;
        D *p2 = static_cast<D*>(p1); // doesn't compile
        D *p3 = reinterpret_cast<D*>(p1); // compiles, but isn't very useful
    }
    

    A plain C-style cast, (B*)(&c) does what you say: if the definition of struct C is visible, showing that B is a base class, then it’s the same as a static_cast. If the types are only forward-declared, then it’s the same as a reinterpret_cast. This is because it’s designed to be compatible with C, meaning that it has to do what C does in cases which are possible in C.

    static_cast always knows what to do for built-in types, that’s really what built-in means. It can convert int to float, and so on. So that’s why it’s always safe for numeric types, but it can’t convert pointers unless (a) it knows what they point to, and (b) there is the right kind of relationship between the pointed-to types. Hence it can convert int to float, but not int* to float*.

    As AndreyT says, there is a way that you can use static_cast unsafely, and the compiler probably won’t save you, because the code is legal:

    A a;
    C *cp = static_cast<C*>(&a); // compiles, undefined behaviour
    

    One of the things static_cast can do is “downcast” a pointer to a derived class (in this case, C is a derived class of A). But if the referand is not actually of the derived class, you’re doomed. A dynamic_cast would perform a check at runtime, but for my example class C you can’t use a dynamic_cast, because A has no virtual functions.

    You can similarly do unsafe things with static_cast to and from void*.

    • 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.