+ -
当前位置:首页 → 问答吧 → C++中析构函数的问题

C++中析构函数的问题

时间:2011-12-11

来源:互联网

为什么析构函数里面不能包含exit()函数。。给个解释。。谢谢。。

作者: zhaokankan   发布时间: 2011-12-11

lz知道exit的作用吗?

作者: Demon__Hunter   发布时间: 2011-12-11

exit 会退出整个程序

由于析构会回收资源


程序退出了。。。资源没有回收,,是很糟糕的方式。。

比如:

作者: seujh2010   发布时间: 2011-12-11

哦,知道了,谢谢啦!
引用 2 楼 seujh2010 的回复:
exit 会退出整个程序

由于析构会回收资源


程序退出了。。。资源没有回收,,是很糟糕的方式。。

比如:

作者: zhaokankan   发布时间: 2011-12-11

exit退出了程序,那么程序所占用的资源会自然被操作系统回收。

问题是,在析构函数里面回收资源,可以让程序在不退出运行的情况下回收资源。试想想,如果一个程序中有10个类,每个类中都写了exit,那么基本上可以说这个程序很难正确地工作,也就是说事情还没做好,程序就退出了。

作者: pathuang68   发布时间: 2011-12-11

exit 直接退出进程。

作者: zhujian888   发布时间: 2011-12-11

exit 
Performs complete C library termination procedures, terminates the process, and exits with the supplied status code. 

结束整个程序。
C/C++ code
/* EXITER.C: This program prompts the user for a yes
 * or no and returns an exit code of 1 if the
 * user answers Y or y; otherwise it returns 0. The
 * error code could be tested in a batch file.
 */

#include <conio.h>
#include <stdlib.h>

void main( void )
{
   int ch;

   _cputs( "Yes or no? " );
   ch = _getch();
   _cputs( "\r\n" );
   if( toupper( ch ) == 'Y' )
      exit( 1 );
   else
      exit( 0 );
}

根据参数的不同判断程序是否为正常终止。

作者: cxxer   发布时间: 2011-12-11

exit是退出整个程序

作者: bsr_nj   发布时间: 2011-12-11

作者: gold_water   发布时间: 2011-12-11