C++ 为什么我的析构函数没有销毁我的对象,它仍然保留其成员变量值?
在我的代码中,我不明白为什么我的派生类对象没有被销毁,即使我显式地调用了析构函数。我不知道这是否是因为我没有包括删除或与范围有关的内容,但你能帮我吗?我在代码中添加了注释,以帮助说明我的问题 在我的头文件中,我有:C++ 为什么我的析构函数没有销毁我的对象,它仍然保留其成员变量值?,c++,destructor,C++,Destructor,在我的代码中,我不明白为什么我的派生类对象没有被销毁,即使我显式地调用了析构函数。我不知道这是否是因为我没有包括删除或与范围有关的内容,但你能帮我吗?我在代码中添加了注释,以帮助说明我的问题 在我的头文件中,我有: #ifndef test_test_h #define test_test_h #include <iostream> #include <string> using namespace std; class test{ public: tes
#ifndef test_test_h
#define test_test_h
#include <iostream>
#include <string>
using namespace std;
class test{
public:
test();
void setName();
string getName();
private:
string name;
};
class book:public test{
public:
book();
~book();
void setBook();
string getBook();
private:
string bookName;
};
#endif
#include "test.h"
test::test()
{
cout<<"calling base construtor"<<endl;
name="hi";
}
book::book()
{
cout<<"Calling derived constructor"<<endl;
bookName="yolo";
}
test::~test()
{
cout<<"calling test destructor"<<endl;
}
book::~book()
{
cout<<"calling book destructor"<<endl;
}
void test::setName()
{
cout<<"Input name"<<endl;
cin>>name;
}
string test::getName()
{
return name;
}
void book::setBook()
{
cout<<"Input name"<<endl;
cin>>bookName;
}
string book::getBook()
{
return bookName;
}
#include "test.h"
int main(){
book b;
b.setBook();
cout<<b.getBook()<<endl;//takes user input
cout<<b.getBook()<<endl;//displays the input u put in
b.~book();
cout<<b.getBook()<<endl;//for some reason it still prints out whatever you inputed even though I called the destructor earlier. I would think it would display yolo because that string is saved in the constructor of book
}
\ifndef测试
#定义测试
#包括
#包括
使用名称空间std;
课堂测试{
公众:
test();
void setName();
字符串getName();
私人:
字符串名;
};
教材:公开考试{
公众:
书();
~book();
无效挫折();
字符串getBook();
私人:
字符串书名;
};
#恩迪夫
在我的实施文件中,我有:
#ifndef test_test_h
#define test_test_h
#include <iostream>
#include <string>
using namespace std;
class test{
public:
test();
void setName();
string getName();
private:
string name;
};
class book:public test{
public:
book();
~book();
void setBook();
string getBook();
private:
string bookName;
};
#endif
#include "test.h"
test::test()
{
cout<<"calling base construtor"<<endl;
name="hi";
}
book::book()
{
cout<<"Calling derived constructor"<<endl;
bookName="yolo";
}
test::~test()
{
cout<<"calling test destructor"<<endl;
}
book::~book()
{
cout<<"calling book destructor"<<endl;
}
void test::setName()
{
cout<<"Input name"<<endl;
cin>>name;
}
string test::getName()
{
return name;
}
void book::setBook()
{
cout<<"Input name"<<endl;
cin>>bookName;
}
string book::getBook()
{
return bookName;
}
#include "test.h"
int main(){
book b;
b.setBook();
cout<<b.getBook()<<endl;//takes user input
cout<<b.getBook()<<endl;//displays the input u put in
b.~book();
cout<<b.getBook()<<endl;//for some reason it still prints out whatever you inputed even though I called the destructor earlier. I would think it would display yolo because that string is saved in the constructor of book
}
#包括“test.h”
test::test()
{
cout首先,对于具有自动存储的变量,不要显式调用析构函数。当您退出定义变量的范围时,将自动调用析构函数
你打电话之后
b.~book();
b
是无效的对象
在这之后使用b
是未定义行为的原因。这之后的行不能保证以任何可预测的方式运行
你有:
cout<<b.getBook()<<endl;//for some reason it still prints out whatever you inputed even though I called the destructor earlier. I would think it would display yolo because that string is saved in the constructor of book
cout
出于某种原因,它仍然会打印出您输入的任何内容,即使我之前调用了析构函数
析构函数方法不会释放对象或清除成员变量使用的内存。它只是一个释放实例生命周期内分配的资源的机会
我认为它会显示yolo,因为该字符串保存在book的构造函数中
这是对象发生的第一件事,而不是最后一件事。对象的状态在销毁后不会返回到构造状态;它是未定义的。最后一次更改可能会保留在内存中
一旦一个对象被运行时破坏,分配给它的内存的内容就没有定义。由于每当一个对象被破坏时,清空内存的成本可能相当高,大多数实现都会保持内存的内容不变,只会让它在以后的使用中被覆盖为什么你在调用了析构函数之后能够打印出书名
以下是main
函数,用一些注释加以说明:
#include "test.h"
int main(){
// 1. Allocate a book object on the stack
book b;
// 2. b.bookName = "yolo"
b.setBook();
cout<<b.getBook()<<endl;
// 3. bookName = "user input"
// Wrong! But calls the destructor method which just prints a message
b.~book();
// 4. displays "user input"
cout<<b.getBook()<<endl;
// b goes out of scope and ~book() will be called automatically
}
#包括“test.h”
int main(){
//1.在堆栈上分配一个book对象
b册;
//2.b.bookName=“yolo”
b、 挫折();
不能直接调用dtor。删除对象时会自动调用dtor。在这里,您可以释放对象的所有内存。@twentylemon没有回答我的问题lol。我的析构函数应该仍然删除我的对象,但它不应该删除对象。b
仍然在范围内。您所做的就是调用名为~book
…的函数,即使在删除对象后,也不意味着这些值一定会消失。访问已删除的内容可能会失败,但删除后可能仍会存在一段时间(直到该内存部分用于其他内容)@user5007232我有没有办法删除对象
你不能用自动存储持续时间控制变量(这是自动存储持续时间的主要原因)。自动构造变量,并在作用域结束时自动销毁。如果你想手动控制它,请使用动态分配。