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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:56:08+00:00 2026-05-13T15:56:08+00:00

I understand that reinterpret_cast is dangerous, I’m just doing this to test it. I

  • 0

I understand that reinterpret_cast is dangerous, I’m just doing this to test it. I have the following code:

int x = 0;
double y = reinterpret_cast<double>(x);

When I try to compile the program, it gives me an error saying

invalid cast from type ‘float’ to type ‘double

What’s going on? I thought reinterpret_cast was the rogue cast that you could use to convert apples to submarines, why won’t this simple cast compile?

  • 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-13T15:56:08+00:00Added an answer on May 13, 2026 at 3:56 pm

    Perhaps a better way of thinking of reinterpret_cast is the rouge operator that can "convert" pointers to apples as pointers to submarines.

    By assigning y to the value returned by the cast you’re not really casting the value x, you’re converting it. That is, y doesn’t point to x and pretend that it points to a float. Conversion constructs a new value of type float and assigns it the value from x. There are several ways to do this conversion in C++, among them:

    int main()
    {
        int x = 42;
        float f = static_cast<float>(x);
        float f2 = (float)x;
        float f3 = float(x);
        float f4 = x;
        return 0;
    }
    

    The only real difference being the last one (an implicit conversion) will generate a compiler diagnostic on higher warning levels. But they all do functionally the same thing — and in many case actually the same thing, as in the same machine code.

    Now if you really do want to pretend that x is a float, then you really do want to cast x, by doing this:

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int x = 42;
        float* pf = reinterpret_cast<float*>(&x);
        (*pf)++;
        cout << *pf;
        return 0;
    }
    

    You can see how dangerous this is. In fact, the output when I run this on my machine is 1, which is decidedly not 42+1.

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

Sidebar

Related Questions

I understand that this a very noob problem, but when I try to create
I understand that this may not necessarily apply to just @properties, but they would
I understand that according to this issue ticket on google code http://code.google.com/p/fullcalendar/issues/detail?id=143 that there
I understand that this question may be subjective, this is why I need an
I understand that we can set the various style attributes from code behind using
I understand that in CUDA's memory hierachy, we have things like shared memory, texture
Okay, I actually don't have code as of yet because i'm just picking out
I understand that a class will have a VTABLE , if it contains at-least
I understand that this is a bad idea for lots of scenarios. I am
I understand that a const pointer can be declared a couple ways: const int

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.