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

    <small id='5NVx3'></small><noframes id='5NVx3'>

      <legend id='5NVx3'><style id='5NVx3'><dir id='5NVx3'><q id='5NVx3'></q></dir></style></legend>
        <bdo id='5NVx3'></bdo><ul id='5NVx3'></ul>
    1. <tfoot id='5NVx3'></tfoot>
      1. 在 C++ 继承中,当指向基类的指针对象指向派生类

        时间:2023-08-02
        <tfoot id='TeF7e'></tfoot>

              <bdo id='TeF7e'></bdo><ul id='TeF7e'></ul>
            • <small id='TeF7e'></small><noframes id='TeF7e'>

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

                    <tbody id='TeF7e'></tbody>
                  本文介绍了在 C++ 继承中,当指向基类的指针对象指向派生类时,不调用派生类析构函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我是新手,我知道这是一个非常基本的概念,也可能是重复的.一旦调用了构造函数,就必须调用其相应的析构函数,这不是真的吗?[代码在 Dev C++ 上运行]

                  I am a newbie and I know this is a very basic concept and might be a duplicate too. Is it not true that once a constructor is called its corresponding destructor has to be called? [code run on Dev C++]

                  class Base
                      {
                       public:
                              Base() { cout<<"Base Constructor
                  ";}
                              int b;
                       ~Base() {cout << "Base Destructor
                  "; }
                      };
                  
                  class Derived:public Base
                  {
                   public:
                          Derived() { cout<<"Derived Constructor
                  ";}
                          int a;
                   ~Derived() { cout<< "Derived Destructor
                  "; }
                  }; 
                  int main () {
                  Base* b = new Derived;    
                  //Derived *b = new Derived;
                   delete b;
                      getch();
                      return 0;
                  }
                  

                  给出输出

                  Base Constructor
                  Derived Constructor
                  Base Destructor
                  

                  推荐答案

                  您的代码有未定义的行为.基类的析构函数必须是 virtual 以便以下具有定义的行为.

                  Your code has undefined behavior. The base class's destructor must be virtual for the following to have defined behavior.

                  Base* b = new Derived;    
                  delete b;
                  

                  来自 C++ 标准:

                  5.3.5 删除

                  3 在第一种选择中(删除对象),如果静态类型的操作数与其动态类型不同,静态类型应为操作数动态类型的基类,静态类型应具有虚析构函数或行为未定义.

                  3 In the first alternative (delete object), if the static type of the operand is different from its dynamic type, the static type shall be a base class of the operand’s dynamic type and the static type shall have a virtual destructor or the behavior is undefined.

                  所以在你的情况下,静态类型是 Base,动态类型是 Derived.所以 Base 的析构函数应该是:

                  So in your case, the static type is Base, and the dynamic type is Derived. So the Base's destructor should be:

                  virtual ~Base() {cout << "Base Destructor
                  "; }
                  

                  这篇关于在 C++ 继承中,当指向基类的指针对象指向派生类时,不调用派生类析构函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:切片在 C++ 中是什么意思? 下一篇:C++ 双调度 Equals()

                  相关文章

                  最新文章

                  <tfoot id='9doHe'></tfoot>

                      <bdo id='9doHe'></bdo><ul id='9doHe'></ul>
                  1. <small id='9doHe'></small><noframes id='9doHe'>

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