C++ 析构函数输出
这是我的密码 在文件Burrito.cpp中C++ 析构函数输出,c++,class,destructor,C++,Class,Destructor,这是我的密码 在文件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"&
#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;}
在主文件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;
}
为什么它会显示两次解构?您定义了类的两个对象
#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;
}
不创建类的对象。指针指向已创建的对象,因此
许多指针可以同时引用同一个对象,但该对象只会被销毁一次
如果你写了一个例子
Burrito *sagar = new Burrito;
然后
delete sagar;
然后在变量sagar的声明中,创建了该类的另一个对象。然后使用运算符delete将其删除。在这种情况下,将调用该对象的析构函数。您定义了该类的两个对象
#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;
}
不创建类的对象。指针指向已创建的对象,因此
许多指针可以同时引用同一个对象,但该对象只会被销毁一次
如果你写了一个例子
Burrito *sagar = new Burrito;
然后
delete sagar;
然后在变量sagar的声明中,创建了该类的另一个对象。然后使用运算符delete将其删除。在这种情况下,将调用对象的析构函数。您正在创建两个Burrito实例 一是玉米煎饼如此; 另一个是康斯特玉米煎饼康斯特物体 所以,当程序完成时,它们被销毁
这就是为什么您要获得两次输出。您要创建两个玉米煎饼实例 一是玉米煎饼如此; 另一个是康斯特玉米煎饼康斯特物体 所以,当程序完成时,它们被销毁
这就是为什么您会得到两次输出。您在主对象和so对象的末尾销毁了object constObject,这两个对象都被销毁了,因此它们都会打印此消息。您在主对象和so对象的末尾销毁了object constObject,两个都被销毁,因此它们都打印此消息。您已经定义了两个Burrito实例—so和constObject;因此,析构函数注意:每个实例调用一次析构函数,而不是解构函数 为了明确创建、操作和销毁的对象,您可能需要修改类以包含名称字段,仅用于演示目的: 玉米煎饼 玉米煎饼
祝你好运。你已经定义了两个玉米煎饼实例——so和constObject;因此,析构函数注意:每个实例调用一次析构函数,而不是解构函数 为了明确创建、操作和销毁的对象,您可能需要修改类以包含名称字段,仅用于演示目的: 玉米煎饼 玉米煎饼
祝你好运。因为你做了两个玉米煎饼。您会期望多于或少于2个析构函数调用吗?您已经定义了两个Burrito实例—so和constObject;因此,析构函数注意:每个实例调用一次析构函数,而不是解构函数。幸运的是你建造了两个玉米煎饼。您会期望多于或少于2个析构函数调用吗?您已经定义了两个Burrito实例—so和constObject;因此,析构函数注意:每个实例调用一次析构函数,而不是解构函数。祝你好运
#ifndef BURRITO_H
#define BURRITO_H
class Burrito
{private: char *name;
public:
Burrito(char *nm);
void printCrap();
void constante() const;
~Burrito();};
#endif // BURRITO_H
#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;}
#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