【问题标题】:Destructor's Output析构函数的输出
【发布时间】:2016-04-28 00:18:00
【问题描述】:

这是我的代码

在 Burrito.cpp 文件中

#include "Burrito.h"
#include <iostream>

using namespace std;

Burrito::Burrito(){}

void Burrito::printCrap(){cout<< "something"<<endl;}

void Burrito::constante() const{cout<<"this aint gonna change"<<endl;}

Burrito::~Burrito(){cout<<"deconstructed"<<endl;}

在 Burrito.h 文件中

#ifndef BURRITO_H
#define BURRITO_H

class Burrito
{public:
        Burrito();
        void printCrap();
        void constante() const;
        ~Burrito();};

#endif // BURRITO_H

在主文件main.cpp中

#include <iostream>
#include "Burrito.h"
using namespace std;

int main()
{
    Burrito so;
    Burrito *sagar = &so;
    so.printCrap();
    sagar->printCrap();
  const Burrito constObject;
    constObject.constante();
    return 0;
}

我是初学者,这是我的问题。我试图了解析构函数,这段代码运行良好,只是它运行了析构函数 2 次,所以这个程序的结果是这样的:

something
something
this aint gonna change
deconstructed
deconstructed

为什么会出现两次“解构”?

【问题讨论】:

  • 因为您构造了两个Burrito 对象。您会期望多于还是少于 2 个析构函数调用?
  • 您已经定义了两个 Burrito 实例 - so 和 constObject;因此,每个实例都会调用一次析构函数(注意:析构函数,而不是“解构函数”)。祝你好运

标签: c++ class destructor


【解决方案1】:

你定义了类的两个对象

#include <iostream>
#include "Burrito.h"
using namespace std;

int main()
{
    Burrito so;
    ^^^^^^^^^^^
    Burrito *sagar = &so;
    so.printCrap();
    sagar->printCrap();

  const Burrito constObject;
  ^^^^^^^^^^^^^^^^^^^^^^^^^^
    constObject.constante();
    return 0;
}

所以这两个对象被破坏了。程序中没有创建该类的其他对象。

指针sagar的声明

Burrito *sagar = &so;

不创建类的对象。指针指向已经创建的对象so

多个指针可以同时指向同一个对象,但该对象只会被销毁一次。

如果你写了例子

Burrito *sagar = new Burrito;

然后

delete sagar;

然后在变量sagar 的声明中又创建了一个类的对象。然后使用运算符delete 可以将其删除。在这种情况下,将调用对象的析构函数。

【讨论】:

    【解决方案2】:

    您正在创建 Burrito() 的 2 个实例。

    一个是Burrito so;
    另一个是const Burrito constObject;

    所以,它们在程序结束时被销毁。

    这就是你得到两次输出的原因。

    【讨论】:

      【解决方案3】:

      您已经定义了两个 Burrito 实例 - so 和 constObject;因此,每个实例都会调用一次析构函数(注意:析构函数,而不是“解构函数”)。

      为了明确创建、操作和销毁哪个对象,您可能需要修改您的类以包含 name 字段,仅用于演示目的:

      Burrito.h

      #ifndef BURRITO_H
      #define BURRITO_H
      
      class Burrito
      {private: char *name;
       public:
              Burrito(char *nm);
              void printCrap();
              void constante() const;
              ~Burrito();};
      
      #endif // BURRITO_H
      

      Burrito.cpp

      #include "Burrito.h"
      #include <iostream>
      
      using namespace std;
      
      Burrito::Burrito(char *nm){ name = nm; cout << "constructed " << name << endl;}
      
      void Burrito::printCrap(){cout<< name << ": something"<<endl;}
      
      void Burrito::constante() const{cout<< name << ": this aint gonna change"<<endl;}
      
      Burrito::~Burrito(){cout<<"destroyed " << name <<endl;}
      

      main.cpp

      #include <iostream>
      #include "Burrito.h"
      using namespace std;
      
      int main()
      {
          Burrito so("so");
          Burrito *sagar = &so;
          so.printCrap();
          sagar->printCrap();
        const Burrito constObject("constObject");
          constObject.constante();
          return 0;
      }
      

      编译运行以上结果:

      constructed so
      so: something
      so: something
      constructed constObject
      constObject: this aint gonna change
      destroyed constObject
      destroyed so
      

      祝你好运。

      【讨论】:

        【解决方案4】:

        您的对象 constObject 在 main 的末尾被销毁,so 对象都被销毁了,因此它们都打印了此消息。

        【讨论】:

          猜你喜欢
          • 2019-05-14
          • 2012-01-13
          • 1970-01-01
          • 2014-08-20
          • 2020-05-24
          • 2015-08-26
          • 2013-01-01
          • 2019-01-14
          相关资源
          最近更新 更多