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

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

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

        转换指针类型的正确方法

        时间:2023-09-26
            <bdo id='bnfmf'></bdo><ul id='bnfmf'></ul>
            <i id='bnfmf'><tr id='bnfmf'><dt id='bnfmf'><q id='bnfmf'><span id='bnfmf'><b id='bnfmf'><form id='bnfmf'><ins id='bnfmf'></ins><ul id='bnfmf'></ul><sub id='bnfmf'></sub></form><legend id='bnfmf'></legend><bdo id='bnfmf'><pre id='bnfmf'><center id='bnfmf'></center></pre></bdo></b><th id='bnfmf'></th></span></q></dt></tr></i><div id='bnfmf'><tfoot id='bnfmf'></tfoot><dl id='bnfmf'><fieldset id='bnfmf'></fieldset></dl></div>
          • <legend id='bnfmf'><style id='bnfmf'><dir id='bnfmf'><q id='bnfmf'></q></dir></style></legend>
            • <tfoot id='bnfmf'></tfoot>

              1. <small id='bnfmf'></small><noframes id='bnfmf'>

                  <tbody id='bnfmf'></tbody>

                  本文介绍了转换指针类型的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑到以下代码(和VirtualAlloc() 返回一个 void*):

                  Considering the following code (and the fact that VirtualAlloc() returns a void*):

                  BYTE* pbNext = reinterpret_cast<BYTE*>(
                      VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE));
                  

                  为什么选择 reinterpret_cast 而不是 static_cast?

                  why is reinterpret_cast chosen instead of static_cast?

                  我曾经认为 reinterpret_cast 可以用于例如将指针转换为整数类型(例如 DWORD_PTR),但是从 void* 转换为 BYTE*,不是 static_cast 好吗?

                  I used to think that reinterpret_cast is OK for e.g. casting pointers to and from integer types (like e.g. DWORD_PTR), but to cast from a void* to a BYTE*, isn't static_cast OK?

                  在这种特殊情况下是否有任何(微妙的?)差异,或者它们只是有效的指针转换?

                  Are there any (subtle?) differences in this particular case, or are they just both valid pointer casts?

                  C++ 标准是否对这种情况有偏好,建议一种方法而不是另一种方法?

                  Does the C++ standard have a preference for this case, suggesting a way instead of the other?

                  推荐答案

                  对于可转换的指向基本类型的指针,两个强制转换具有相同的含义;所以你说 static_cast 没问题是正确的.

                  For convertible pointers to fundamental types both casts have the same meaning; so you are correct that static_cast is okay.

                  在某些指针类型之间进行转换时,可能需要更改指针中保存的特定内存地址.

                  When converting between some pointer types, it's possible that the specific memory address held in the pointer needs to change.

                  这就是两个演员表不同的地方.static_cast 会做适当的调整.reinterpret_cast 不会.

                  That's where the two casts differ. static_cast will make the appropriate adjustment. reinterpret_cast will not.

                  因此,除非您知道需要reinterpret_cast,否则在指针类型之间static_cast 是一个很好的一般规则.

                  For that reason, it's a good general rule to static_cast between pointer types unless you know that reinterpret_cast is desired.

                  这篇关于转换指针类型的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++ 不能通过虚基 A 从基 A 转换为派生类型 B 下一篇:在 C 中将双精度转换为整数时处理溢出

                  相关文章

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

                      <legend id='YIe90'><style id='YIe90'><dir id='YIe90'><q id='YIe90'></q></dir></style></legend>
                    1. <tfoot id='YIe90'></tfoot>
                    2. <small id='YIe90'></small><noframes id='YIe90'>

                      • <bdo id='YIe90'></bdo><ul id='YIe90'></ul>