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

    1. <tfoot id='vFHdI'></tfoot>

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

        <bdo id='vFHdI'></bdo><ul id='vFHdI'></ul>
      <i id='vFHdI'><tr id='vFHdI'><dt id='vFHdI'><q id='vFHdI'><span id='vFHdI'><b id='vFHdI'><form id='vFHdI'><ins id='vFHdI'></ins><ul id='vFHdI'></ul><sub id='vFHdI'></sub></form><legend id='vFHdI'></legend><bdo id='vFHdI'><pre id='vFHdI'><center id='vFHdI'></center></pre></bdo></b><th id='vFHdI'></th></span></q></dt></tr></i><div id='vFHdI'><tfoot id='vFHdI'></tfoot><dl id='vFHdI'><fieldset id='vFHdI'></fieldset></dl></div>
      1. 使用 nullptr 有什么好处?

        What are the advantages of using nullptr?(使用 nullptr 有什么好处?)

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

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

            <tbody id='tDyqJ'></tbody>
            • <legend id='tDyqJ'><style id='tDyqJ'><dir id='tDyqJ'><q id='tDyqJ'></q></dir></style></legend>
                <bdo id='tDyqJ'></bdo><ul id='tDyqJ'></ul>
                <tfoot id='tDyqJ'></tfoot>

                1. 本文介绍了使用 nullptr 有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这段代码概念上对三个指针做了同样的事情(安全指针初始化):

                  This piece of code conceptually does the same thing for the three pointers (safe pointer initialization):

                  int* p1 = nullptr;
                  int* p2 = NULL;
                  int* p3 = 0;
                  

                  那么,分配指针 nullptr 与分配值 NULL0 相比有什么优势?

                  And so, what are the advantages of assigning pointers nullptr over assigning them the values NULL or 0?

                  推荐答案

                  在那段代码中,似乎没有什么优势.但请考虑以下重载函数:

                  In that code, there doesn't seem to be an advantage. But consider the following overloaded functions:

                  void f(char const *ptr);
                  void f(int v);
                  
                  f(NULL);  //which function will be called?
                  

                  会调用哪个函数?当然,这里的意图是调用f(char const *),但实际上会调用f(int)!这是个大问题1,不是吗?

                  Which function will be called? Of course, the intention here is to call f(char const *), but in reality f(int) will be called! That is a big problem1, isn't it?

                  所以,解决此类问题的方法是使用nullptr:

                  So, the solution to such problems is to use nullptr:

                  f(nullptr); //first function is called
                  

                  <小时>

                  当然,这不是 nullptr 的唯一优势.这是另一个:


                  Of course, that is not the only advantage of nullptr. Here is another:

                  template<typename T, T *ptr>
                  struct something{};                     //primary template
                  
                  template<>
                  struct something<nullptr_t, nullptr>{};  //partial specialization for nullptr
                  

                  由于在模板中,nullptr的类型被推导出为nullptr_t,所以你可以这样写:

                  Since in template, the type of nullptr is deduced as nullptr_t, so you can write this:

                  template<typename T>
                  void f(T *ptr);   //function to handle non-nullptr argument
                  
                  void f(nullptr_t); //an overload to handle nullptr argument!!!
                  

                  <小时>

                  1.在C++中,NULL被定义为#define NULL 0,所以基本上是int,这就是为什么f(int) 被调用.


                  1. In C++, NULL is defined as #define NULL 0, so it is basically int, that is why f(int) is called.

                  这篇关于使用 nullptr 有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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++ 引用发生变化)
                  <i id='v7hZg'><tr id='v7hZg'><dt id='v7hZg'><q id='v7hZg'><span id='v7hZg'><b id='v7hZg'><form id='v7hZg'><ins id='v7hZg'></ins><ul id='v7hZg'></ul><sub id='v7hZg'></sub></form><legend id='v7hZg'></legend><bdo id='v7hZg'><pre id='v7hZg'><center id='v7hZg'></center></pre></bdo></b><th id='v7hZg'></th></span></q></dt></tr></i><div id='v7hZg'><tfoot id='v7hZg'></tfoot><dl id='v7hZg'><fieldset id='v7hZg'></fieldset></dl></div>
                2. <legend id='v7hZg'><style id='v7hZg'><dir id='v7hZg'><q id='v7hZg'></q></dir></style></legend><tfoot id='v7hZg'></tfoot>

                      <tbody id='v7hZg'></tbody>
                    • <bdo id='v7hZg'></bdo><ul id='v7hZg'></ul>

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