<bdo id='1IrcM'></bdo><ul id='1IrcM'></ul>
      <legend id='1IrcM'><style id='1IrcM'><dir id='1IrcM'><q id='1IrcM'></q></dir></style></legend>

      <small id='1IrcM'></small><noframes id='1IrcM'>

      <i id='1IrcM'><tr id='1IrcM'><dt id='1IrcM'><q id='1IrcM'><span id='1IrcM'><b id='1IrcM'><form id='1IrcM'><ins id='1IrcM'></ins><ul id='1IrcM'></ul><sub id='1IrcM'></sub></form><legend id='1IrcM'></legend><bdo id='1IrcM'><pre id='1IrcM'><center id='1IrcM'></center></pre></bdo></b><th id='1IrcM'></th></span></q></dt></tr></i><div id='1IrcM'><tfoot id='1IrcM'></tfoot><dl id='1IrcM'><fieldset id='1IrcM'></fieldset></dl></div>

        <tfoot id='1IrcM'></tfoot>

        为什么这个 reinterpret_cast 不能编译?

        Why doesn#39;t this reinterpret_cast compile?(为什么这个 reinterpret_cast 不能编译?)

            <tbody id='6GlUx'></tbody>

          <tfoot id='6GlUx'></tfoot>
          <legend id='6GlUx'><style id='6GlUx'><dir id='6GlUx'><q id='6GlUx'></q></dir></style></legend>
            • <small id='6GlUx'></small><noframes id='6GlUx'>

              1. <i id='6GlUx'><tr id='6GlUx'><dt id='6GlUx'><q id='6GlUx'><span id='6GlUx'><b id='6GlUx'><form id='6GlUx'><ins id='6GlUx'></ins><ul id='6GlUx'></ul><sub id='6GlUx'></sub></form><legend id='6GlUx'></legend><bdo id='6GlUx'><pre id='6GlUx'><center id='6GlUx'></center></pre></bdo></b><th id='6GlUx'></th></span></q></dt></tr></i><div id='6GlUx'><tfoot id='6GlUx'></tfoot><dl id='6GlUx'><fieldset id='6GlUx'></fieldset></dl></div>
                  <bdo id='6GlUx'></bdo><ul id='6GlUx'></ul>
                • 本文介绍了为什么这个 reinterpret_cast 不能编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道 reinterpret_cast 很危险,我只是为了测试它.我有以下代码:

                  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

                  从float"类型到double"类型的无效转换

                  invalid cast from type 'float' to type 'double

                  这是怎么回事?我认为 reinterpret_cast 是你可以用来将苹果转换为潜艇的流氓演员,为什么这个简单的演员不能编译?

                  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?

                  推荐答案

                  也许对 reinterpret_cast 更好的思考方式是胭脂运算符,它可以转换"指向苹果就像指向潜艇一样.

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

                  通过将 y 分配给转换返回的值,您实际上并不是在转换值 x,而是在转换它.也就是说,y 不指向 x 并假装它指向一个浮点数.转换构造了一个 float 类型的新值,并将 x 中的值赋给它.在 C++ 中有几种方法可以进行这种转换,其中包括:

                  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.

                  现在如果你真的想假装 x 是一个浮点数,那么你真的想通过这样做来转换 x:

                  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;
                  }
                  

                  你可以看到这是多么危险.事实上,当我在我的机器上运行它时的输出是 1,这绝对不是 42+1.

                  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.

                  这篇关于为什么这个 reinterpret_cast 不能编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)
                    • <bdo id='d623X'></bdo><ul id='d623X'></ul>
                      <tfoot id='d623X'></tfoot>

                        <legend id='d623X'><style id='d623X'><dir id='d623X'><q id='d623X'></q></dir></style></legend>

                        <small id='d623X'></small><noframes id='d623X'>

                        <i id='d623X'><tr id='d623X'><dt id='d623X'><q id='d623X'><span id='d623X'><b id='d623X'><form id='d623X'><ins id='d623X'></ins><ul id='d623X'></ul><sub id='d623X'></sub></form><legend id='d623X'></legend><bdo id='d623X'><pre id='d623X'><center id='d623X'></center></pre></bdo></b><th id='d623X'></th></span></q></dt></tr></i><div id='d623X'><tfoot id='d623X'></tfoot><dl id='d623X'><fieldset id='d623X'></fieldset></dl></div>
                            <tbody id='d623X'></tbody>