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

  • <tfoot id='2Mel2'></tfoot><legend id='2Mel2'><style id='2Mel2'><dir id='2Mel2'><q id='2Mel2'></q></dir></style></legend>

      <small id='2Mel2'></small><noframes id='2Mel2'>

          <bdo id='2Mel2'></bdo><ul id='2Mel2'></ul>

        你在 C++ 中使用 NULL 还是 0(零)作为指针?

        Do you use NULL or 0 (zero) for pointers in C++?(你在 C++ 中使用 NULL 还是 0(零)作为指针?)
          <tbody id='7dNkV'></tbody>
      1. <legend id='7dNkV'><style id='7dNkV'><dir id='7dNkV'><q id='7dNkV'></q></dir></style></legend>
        <tfoot id='7dNkV'></tfoot>
      2. <i id='7dNkV'><tr id='7dNkV'><dt id='7dNkV'><q id='7dNkV'><span id='7dNkV'><b id='7dNkV'><form id='7dNkV'><ins id='7dNkV'></ins><ul id='7dNkV'></ul><sub id='7dNkV'></sub></form><legend id='7dNkV'></legend><bdo id='7dNkV'><pre id='7dNkV'><center id='7dNkV'></center></pre></bdo></b><th id='7dNkV'></th></span></q></dt></tr></i><div id='7dNkV'><tfoot id='7dNkV'></tfoot><dl id='7dNkV'><fieldset id='7dNkV'></fieldset></dl></div>
          • <bdo id='7dNkV'></bdo><ul id='7dNkV'></ul>

              <small id='7dNkV'></small><noframes id='7dNkV'>

                  本文介绍了你在 C++ 中使用 NULL 还是 0(零)作为指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 C++ 的早期,当它被栓在 C 之上时,您不能使用 NULL,因为它被定义为 (void*)0.除了 void* 之外,您不能将 NULL 分配给任何指针,这使它变得毫无用处.在那些日子里,您使用 0(零)作为空指针是公认的.

                  In the early days of C++ when it was bolted on top of C, you could not use NULL as it was defined as (void*)0. You could not assign NULL to any pointer other than void*, which made it kind of useless. Back in those days, it was accepted that you used 0 (zero) for null pointers.

                  直到今天,我继续使用零作为空指针,但我周围的人坚持使用NULL.我个人认为给现有值命名 (NULL) 没有任何好处 - 而且因为我也喜欢将指针测试为真值:

                  To this day, I have continued to use zero as a null pointer but those around me insist on using NULL. I personally do not see any benefit to giving a name (NULL) to an existing value - and since I also like to test pointers as truth values:

                  if (p && !q)
                    do_something();
                  

                  然后使用零更有意义(就像如果你使用 NULL,你不能在逻辑上使用 p && !q - 你需要明确地与 NULL,除非你假设 NULL 为零,在这种情况下为什么要使用 NULL).

                  then using zero makes more sense (as in if you use NULL, you cannot logically use p && !q - you need to explicitly compare against NULL, unless you assume NULL is zero, in which case why use NULL).

                  是否有任何客观原因更喜欢零而不是 NULL(反之亦然),或者只是个人喜好?

                  Is there any objective reason to prefer zero over NULL (or vice versa), or is all just personal preference?

                  我应该补充(并且原本打算说)对于 RAII 和异常,我很少使用零/NULL 指针,但有时您仍然需要它们.

                  I should add (and meant to originally say) that with RAII and exceptions, I rarely use zero/NULL pointers, but sometimes you do need them still.

                  推荐答案

                  这是 Stroustrup 对此的看法:C++ 风格和技术常见问题解答

                  Here's Stroustrup's take on this: C++ Style and Technique FAQ

                  在 C++ 中,NULL 的定义是 0,所以只是审美差异.我更喜欢避免使用宏,所以我使用 0.NULL 的另一个问题是人们有时会错误地认为它不同于 0 和/或不是整数.在标准前的代码中,NULL 有时被/被定义为不合适的东西,因此不得不/必须避免.现在这种情况不太常见了.

                  In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.

                  如果一定要给空指针命名,就叫它nullptr;这就是它在 C++11 中的名称.然后,nullptr 将是一个关键字.

                  If you have to name the null pointer, call it nullptr; that's what it's called in C++11. Then, nullptr will be a keyword.

                  也就是说,不要为小事操心.

                  That said, don't sweat the small stuff.

                  这篇关于你在 C++ 中使用 NULL 还是 0(零)作为指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                    <tbody id='3MDWs'></tbody>

                      • <bdo id='3MDWs'></bdo><ul id='3MDWs'></ul>

                        <small id='3MDWs'></small><noframes id='3MDWs'>

                        <tfoot id='3MDWs'></tfoot>

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