<tfoot id='HamxB'></tfoot>
      <bdo id='HamxB'></bdo><ul id='HamxB'></ul>
  • <small id='HamxB'></small><noframes id='HamxB'>

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

        为什么在C++中通过空指针调用成员函数时程序不会崩溃?

        Why doesn#39;t the program crash when I call a member function through a null pointer in C++?(为什么在C++中通过空指针调用成员函数时程序不会崩溃?)

          <tbody id='dUZRy'></tbody>

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

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

                  <bdo id='dUZRy'></bdo><ul id='dUZRy'></ul>
                  本文介绍了为什么在C++中通过空指针调用成员函数时程序不会崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  #include "iostream"
                  using namespace std;
                  class A
                  {
                  public:
                      void mprint()
                      {
                          cout<<"
                   TESTING NULL POINTER";
                      }
                  };
                  
                  int main()
                  {
                      A *a = NULL;
                      a->mprint();
                      return 0;
                  }
                  

                  我得到的输出是TESTING NULL POINTER".任何人都可以解释为什么这个程序打印输出而不是崩溃.我在 Dev C++ 和 aCC 编译器上检查过都给出了相同的结果.

                  I am getting output as "TESTING NULL POINTER". Can anyone please explain why this program is printing the output instead of crashing. I checked it on Dev C++ and aCC compiler both gave same result.

                  推荐答案

                  您没有使用 A 的任何成员变量 - 该函数完全独立于 A实例,因此生成的代码碰巧不包含取消引用 0 的任何内容.这仍然是未定义行为 - 它可能恰好在某些编译器上工作.未定义的行为意味着任何事情都可能发生"——包括程序碰巧按照程序员的预期工作.

                  You're not using any member variables of A - the function is completely independent of the A instance, and therefore the generated code happens to not contain anything that dereferences 0. This is still undefined behavior - it just may happen to work on some compilers. Undefined behavior means "anything can happen" - including that the program happens to work as the programmer expected.

                  如果你例如使 mprint 成为虚拟的,您可能会崩溃 - 或者如果编译器发现它并不真正需要 vtable,您可能不会崩溃.

                  If you e.g. make mprint virtual you may get a crash - or you may not get one if the compiler sees that it doesn't really need a vtable.

                  如果你给 A 添加一个成员变量并打印出来,你会崩溃.

                  If you add a member variable to A and print this, you will get a crash.

                  这篇关于为什么在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++ 引用发生变化)
                • <tfoot id='AfIgd'></tfoot>

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

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

                          <tbody id='AfIgd'></tbody>
                          <bdo id='AfIgd'></bdo><ul id='AfIgd'></ul>