【发布时间】:2016-01-23 16:28:57
【问题描述】:
我意识到这个问题已经出现过几次,但我正在努力为上述问题找到一个明确的答案,但我不断遇到相互矛盾的信息。我需要知道的是,当我使用 exit() 时,基本类对象是否被破坏。我知道需要删除动态内存,但我的意思更像是:
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class employee
{
public:
employee ();
string name;
~employee();
};
employee::employee ()
{
name = "bob";
}
employee::~employee()
{
cout << "Object destroyed" << endl;
}
int main()
{
employee emp1;
exit(1);
cout << "Hello" << endl;
}
现在,如果我从 main 中删除 exit(1),“对象已销毁”和“Hello”将按预期打印。但是,将其保留在那里,两者都没有被打印出来。 “Hello”的原因很明显,但我的印象是 emp1 仍然会被破坏,但没有显示破坏消息......
我在看this link,它说静态对象被破坏。上面的对象不被认为是静态的吗?
如果没有,有没有办法让程序在不占用内存的情况下终止?我的项目围绕用户输入展开,如果用户输入“退出”一词,我试图提供退出选项。
if(input_var == "exit")
{
cout << "You have chosen to exit the program." << endl;
exit(1);
}
我的意图是一个粗略的例子。
【问题讨论】:
标签: c++ class destructor exit