| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 678 人关注过本帖
标题:关于devc++中,析构函数的问题
收藏  订阅  推荐  打印 
ljhwahaha
Rank: 1
等级:新手上路
帖子:28
积分:380
注册:2007-3-13
关于devc++中,析构函数的问题

我用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"); 的影响吗?有办法解决吗?

搜索更多相关主题的帖子: devc  函数  
2007-3-13 21:51
yuyunliuhen
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:20
帖子:1419
积分:14466
注册:2005-12-12

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


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-3-13 22:06
ljhwahaha
Rank: 1
等级:新手上路
帖子:28
积分:380
注册:2007-3-13

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

2007-3-13 22:12
yuyunliuhen
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:20
帖子:1419
积分:14466
注册:2005-12-12

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


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-3-13 22:20
ljhwahaha
Rank: 1
等级:新手上路
帖子:28
积分:380
注册:2007-3-13


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

还有没有其他办法?? 能不用system("PAUSE"); 吗?
2007-3-13 22:23
yuyunliuhen
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:20
帖子:1419
积分:14466
注册:2005-12-12

可以啊,你不用system("PAUSE");结果也是一样的...

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-3-13 22:24
ljhwahaha
Rank: 1
等级:新手上路
帖子:28
积分:380
注册:2007-3-13

但输出结果的框一弹出来就没了,很不方便。 除了system("PAUSE"); ,还有没有其他能保留结果输出框的方法吖?
2007-3-13 22:29
song4
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:38
帖子:1506
积分:15212
注册:2006-3-25

你可以用
getch()试试


我说自尊那 看起来或许可笑 但它至少支着我 试着不让我颠倒 活着 如果只是不甘寂静的喧嚣 那就咆哮吧 让每个人都听得到学习JAVA
2007-3-13 22:36
yuyunliuhen
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:20
帖子:1419
积分:14466
注册:2005-12-12




上面分别是用了system("PAUSE");和没有用system("PAUSE"); 的区别
附件: 只有本站会员才能下载或查看附件,请您 登录注册

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-3-13 22:38
ljhwahaha
Rank: 1
等级:新手上路
帖子:28
积分:380
注册:2007-3-13

你是用devc++ 的吗? 我不用system("PAUSE"); 的话,根本保留不了输出框喔```
2007-3-13 22:44
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.051084 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved