Tony_Xian 2017年05月07日
关于system("pause")和析构函数执行
#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"constructing A"<<endl;
}
~A()
{
cout<<"destructing A"<<endl;
}
};

class B:public A
{
public:
B()
{
cout<<"constructing B"<<endl;
}
~B()
{
cout<<"destructing B"<<endl;
}
};

class C:public B
{
public:
C()
{
cout<<"constructing C"<<endl;
}
~C()
{
cout<<"destructing C"<<endl;
}
};

int main()
{
C c1;
system("pause");
return 0;
}


编译器不显示析构函数,但我感觉是执行了的,请问为什么会这样?

修改代码后如下:
#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"constructing A"<<endl;
}
~A()
{
cout<<"destructing A"<<endl;
}
};

class B:public A
{
public:
B()
{
cout<<"constructing B"<<endl;
}
~B()
{
cout<<"destructing B"<<endl;
}
};

class C:public B
{
public:
C()
{
cout<<"constructing C"<<endl;
}
~C()
{
cout<<"destructing C"<<endl;
}
};

void end_pause()
{
system("pause");
}

int main()
{
atexit(end_pause);
C c1;
return 0;
}

此时编译器显示析构函数,请问这次为什么能显示?
谢谢各位解答的大兄弟了
...全文
114 点赞 收藏 4
写回复
4 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C++ 语言
创建于2007-09-28

3.1w+

社区成员

24.8w+

社区内容

C++ 语言相关问题讨论,技术干货分享
社区公告
暂无公告