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

          <bdo id='dSpng'></bdo><ul id='dSpng'></ul>

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

        <legend id='dSpng'><style id='dSpng'><dir id='dSpng'><q id='dSpng'></q></dir></style></legend>
      1. <tfoot id='dSpng'></tfoot>

        在 C++ 中检查空对象

        Checking for a null object in C++(在 C++ 中检查空对象)
          <tfoot id='vtJ4g'></tfoot>
          • <bdo id='vtJ4g'></bdo><ul id='vtJ4g'></ul>

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

              <tbody id='vtJ4g'></tbody>

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

                  <legend id='vtJ4g'><style id='vtJ4g'><dir id='vtJ4g'><q id='vtJ4g'></q></dir></style></legend>
                • 本文介绍了在 C++ 中检查空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我大部分时间只使用 C,并且在 C++ 中遇到了一些不熟悉的问题.

                  I've mostly only worked with C and am running into some unfamiliar issues in C++.

                  假设我在 C 中有一些这样的函数,这将是非常典型的:

                  Let's say that I have some function like this in C, which would be very typical:

                  int some_c_function(const char* var)
                  {
                      if (var == NULL) {
                          /* Exit early so we don't dereference a null pointer */
                      }
                      /* The rest of the code */
                  }
                  

                  假设我正在尝试用 C++ 编写一个类似的函数:

                  And let's say that I'm trying to write a similar function in C++:

                  int some_cpp_function(const some_object& str)
                  {
                      if (str == NULL)  // This doesn't compile, probably because some_object doesn't overload the == operator
                  
                      if (&str == NULL) // This compiles, but it doesn't work, and does this even mean anything?
                  }
                  

                  基本上,我试图做的就是在使用 NULL 调用 some_cpp_function() 时防止程序崩溃.

                  Basically, all I'm trying to do is to prevent the program from crashing when some_cpp_function() is called with NULL.

                  • 使用对象 C++ 执行此操作的最典型/最常见的方法是什么(不涉及重载 == 运算符)?

                  这甚至是正确的方法吗?也就是说,我不应该编写将对象作为参数的函数,而是编写成员函数?(但即使是这样,请回答原始问题)

                  Is this even the right approach? That is, should I not write functions that take an object as an argument, but rather, write member functions? (but even if so, please answer the original question)

                  在接受对象引用的函数,或接受指向对象的 C 风格指针的函数之间,是否有理由选择一个而不是另一个?

                  Between a function that takes a reference to an object, or a function that takes a C-style pointer to an object, are there reasons to choose one over the other?

                  推荐答案

                  基本上,我要做的就是防止程序崩溃时some_cpp_function() 被调用空.

                  Basically, all I'm trying to do is to prevent the program from crashing when some_cpp_function() is called with NULL.

                  不能用 NULL 调用函数.拥有引用的目的之一是,它将始终指向某个对象,因为您必须在定义它时对其进行初始化.不要将引用视为一个花哨的指针,将其视为对象本身的别名.那么这种混淆就不会出现了.

                  It is not possible to call the function with NULL. One of the purpose of having the reference, it will point to some object always as you have to initialize it when defining it. Do not think reference as a fancy pointer, think of it as an alias name for the object itself. Then this type of confusion will not arise.

                  这篇关于在 C++ 中检查空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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++ 引用发生变化)

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

                        • <bdo id='lM59i'></bdo><ul id='lM59i'></ul>
                          <tfoot id='lM59i'></tfoot>