注册 登录
编程论坛 C++教室

关于devc++中,析构函数的问题

ljhwahaha 发布于 2007-03-13 21:51, 1800 次点击

我用devc++编写了下列程序用于检验类中构造函数和析构函数的运行时间

#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;
class Date
{
public:
Date ();
~Date ();
void SetDate(int y,int m,int d);
int IsLeapYear()const;
void PrintDate()const;
private:
int year,month,day;
};
Date:: Date () //构造函数
{cout<<"Date object initialized.\n";}
Date:: ~ Date() //析构函数
{cout<<"Date object destroyed.\n";}
void Date:: SetDate(int y, int m, int d)
{year=y;month=m;day=d;}
int Date:: IsLeapYear() const
{
return(year%4==0&&year%100!=0)||(year%400==0);
}
void Date:: PrintDate() const
{cout<<year<<"/"<<month<<"/"<<day<<endl;}

int main(int argc, char *argv[])
{
Date d;
d.SetDate(2001,10,1);
d.PrintDate();
system("PAUSE");
return EXIT_SUCCESS;
}

但输出的结构并没有 Date object destroyed. 这一句,也就是说没执行到析构函数,为什么会这样呢? 是因为 system("PAUSE"); 的影响吗?有办法解决吗?

15 回复
#2
yuyunliuhen2007-03-13 22:06

system("PAUSE"); 键入回车后 Date object destroyed 还是会输出的.应该是与函数调用顺序有关吧

#3
ljhwahaha2007-03-13 22:12

键入回车后,那个输出结果的窗口就没了,应该说键入任意键那个窗口都会消失````仍然看不到吖

#4
yuyunliuhen2007-03-13 22:20

是呀,在那一瞬间还是会输出的啊,虽然不能看到.system("PAUSE"); 是不会影响程序运行结果的,你可以换个编译器试一下,会输出来

#5
ljhwahaha2007-03-13 22:23

怎样换编译器吖? 我不会这个吖。

还有没有其他办法?? 能不用system("PAUSE"); 吗?
#6
yuyunliuhen2007-03-13 22:24
可以啊,你不用system("PAUSE");结果也是一样的...
#7
ljhwahaha2007-03-13 22:29
但输出结果的框一弹出来就没了,很不方便。 除了system("PAUSE"); ,还有没有其他能保留结果输出框的方法吖?
#8
song42007-03-13 22:36

你可以用
getch()试试

#9
yuyunliuhen2007-03-13 22:38
只有本站会员才能查看附件,请 登录

只有本站会员才能查看附件,请 登录


上面分别是用了system("PAUSE");和没有用system("PAUSE"); 的区别
#10
ljhwahaha2007-03-13 22:44
你是用devc++ 的吗? 我不用system("PAUSE"); 的话,根本保留不了输出框喔```
#11
litcatyx2007-03-13 22:50
直接在命令提示符里输入不就行了
#12
ljhwahaha2007-03-13 22:53
以下是引用song4在2007-3-13 22:36:58的发言:

你可以用
getch()试试

很奇怪,如果用getch()的话,输出结果也只有
Dateobject initialized.
2001/10/1

#13
yuyunliuhen2007-03-13 23:25


很奇怪,如果用getch()的话,输出结果也只有
Dateobject initialized.
2001/10/1

建议查一下MSDN.

如:
#include <conio.h>
#include <ctype.h>

int main( void )
{
int ch;

_cputs( "Type 'Y' when finished typing keys: " );
do
{
ch = _getch();
ch = toupper( ch );
} while( ch != 'Y' );

_putch( ch );
_putch( '\r' ); // Carriage return
_putch( '\n' ); // Line feed
}

[此贴子已经被作者于2007-3-13 23:28:25编辑过]

#14
ljhwahaha2007-03-13 23:51

请问怎样插吖?我的vc++用不了

#15
boyyang48942007-03-15 20:15

#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;
class Date
{
public:
Date ();
~Date ();
void SetDate(int y,int m,int d);
int IsLeapYear()const;
void PrintDate()const;
private:
int year,month,day;
};
Date:: Date () //构造函数
{cout<<"Date object initialized.\n";

}
Date:: ~ Date() //析构函数
{cout<<"Date object destroyed.\n";
system("PAUSE");//在此加入该语句;
}
void Date:: SetDate(int y, int m, int d)
{year=y;month=m;day=d;}
int Date:: IsLeapYear() const
{
return(year%4==0&&year%100!=0)||(year%400==0);
}
void Date:: PrintDate() const
{cout<<year<<"/"<<month<<"/"<<day<<endl;}

int main(int argc, char *argv[])
{
Date d;
d.SetDate(2001,10,1);
d.PrintDate();
system("PAUSE");

return EXIT_SUCCESS;
}

#16
ljhwahaha2007-03-15 22:55

嗯嗯,是好办法!!!
还有个办法就是构造一个函数,将
Date d;
d.SetDate(2001,10,1);
d.PrintDate();
system("PAUSE");
都放到里面,再在main里面调用

1