析构函数的输出
Destructor's Output
这是我的代码
在文件中卷饼.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;}
在文件中 卷饼.h
#ifndef BURRITO_H
#define BURRITO_H
class Burrito
{public:
Burrito();
void printCrap();
void constante() const;
~Burrito();};
#endif // BURRITO_H
在主文件主文件中.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;
}
我是初学者,这是我的查询。我试图了解desctructor,这段代码表现良好,除了它运行析构函数2次,所以这个程序的结果是这样的:
something
something
this aint gonna change
deconstructed
deconstructed
为什么它显示两次"去结构"?
您定义了类的两个对象
#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()
实例。
一个是Burrito so;
另一个是const Burrito constObject;
因此,当程序完成时,它们将被销毁。
这就是您获得两次输出的原因。
您已经定义了两个 Burrito 实例 - so 和 constObject;因此,每个实例调用一次析构函数(注意:析构函数,而不是"解构函数")。
为了明确正在创建、操作和销毁的对象,您可能需要修改类以包含name
字段,仅用于演示目的:
墨西哥卷饼
#ifndef BURRITO_H
#define BURRITO_H
class Burrito
{private: char *name;
public:
Burrito(char *nm);
void printCrap();
void constante() const;
~Burrito();};
#endif // BURRITO_H
墨西哥卷饼.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;}
主.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
祝你好运。
您在主 AND 和so
对象的末尾constObject
销毁了对象,两者都被销毁,因此它们都打印了此消息。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 内联映射初始化的动态atexit析构函数崩溃
- 什么时候调用析构函数
- 优先顺序:智能指针和类析构函数
- C++-明确何时以及如何调用析构函数
- 使用基类指针创建对象时,缺少派生类析构函数
- 在c++中使用向量时,如何调用构造函数和析构函数
- 重载运算符new[]的行为取决于析构函数
- 我需要知道编译器如何在cpp中使用析构函数
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 析构函数调用
- 通过引用传递-为什么要调用这个析构函数
- 析构函数的调用.和输入输出过载
- 析构函数的输出
- 为什么我的输出流seg出错,而我的虚拟析构函数不起作用,但当我杀死虚拟的时候,它起作用了
- c++从程序集输出的角度理解虚析构函数如何增加类型的大小
- c++汇编输出中的重复析构函数
- 应该将输出操作放入析构函数中吗?
- 析构函数给出我不明白的输出("./a.out"中的错误:双重释放或损坏(快速顶部):)