| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1800 人关注过本帖
标题:关于devc++中,析构函数的问题
只看楼主 加入收藏
ljhwahaha
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-3-13
收藏
 问题点数:0 回复次数:15 
关于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-03-13 21:51
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

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-03-13 22:06
ljhwahaha
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-3-13
收藏
得分:0 

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

2007-03-13 22:12
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

是呀,在那一瞬间还是会输出的啊,虽然不能看到.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-03-13 22:20
ljhwahaha
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-3-13
收藏
得分:0 

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

还有没有其他办法?? 能不用system("PAUSE"); 吗?
2007-03-13 22:23
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 
可以啊,你不用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-03-13 22:24
ljhwahaha
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-3-13
收藏
得分:0 
但输出结果的框一弹出来就没了,很不方便。 除了system("PAUSE"); ,还有没有其他能保留结果输出框的方法吖?
2007-03-13 22:29
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 

你可以用
getch()试试


嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2007-03-13 22:36
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册


上面分别是用了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-03-13 22:38
ljhwahaha
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-3-13
收藏
得分:0 
你是用devc++ 的吗? 我不用system("PAUSE"); 的话,根本保留不了输出框喔```
2007-03-13 22:44
快速回复:关于devc++中,析构函数的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013057 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved