#include <iostream>
#include <cstdlib>
#include "File1.h"
using namespace std;
class CDateinfo
{
public:
CDateinfo();
CDateinfo(int year, int month, int day);
~CDateinfo();
void SetDate(int year, int month, int day);
void GetDate();
private:
int year;
int month;
int day;
};
CDateinfo::CDateinfo()
{
this->year = 2007;
this->month = 5;
this->day = 23;
}
CDateinfo::CDateinfo(int year, int month, int day)
{
this->year = year;
this->month = month;
this->day = day;
}
CDateinfo::~CDateinfo()
{
cout << "Unload me" << endl;
this->year = 0;
this->month = 0;
this->day = 0;
}
void CDateinfo::SetDate(int year, int month, int day)
{
this->year = year;
this->month = month;
this->day = day;
}
void CDateinfo::GetDate()
{
cout << "当前日期为: " << this->year << "年"
<< this->month << "月"
<< this->day << "日"
<<endl;
}
int main()
{
CDateinfo* MyDate = new CDateinfo;
MyDate->GetDate();
cout << "执行delete后自动调用析构函数" << endl;
delete MyDate;
cout << endl << endl;
CDateinfo* ThisDate = new CDateinfo(2007, 5, 23);
ThisDate->GetDate();
delete ThisDate;
cout << endl <<endl;
CDateinfo* OtherDate = new CDateinfo;
OtherDate->SetDate(2008,8,8);
OtherDate->GetDate();
delete OtherDate;
cout << endl << endl;
cout << "注意delete OtherDate 后再次调用 OtherDate 的GetDate方法" << endl;
OtherDate->GetDate();
system("pause");
return 0;
}
//---------------------------------------------------------------------------
在类中的析构函数中我加入了
this->year = 0;
this->month = 0;
this->day = 0;
然后delete了OtherDate后再调用GetDate方法,我在dev-c中运行的结果是 0年0月0日的,其它的编译器就是乱来的.
然后我在dev-c中调试,单步进入,到了this->year = 0;这句之后就不能再运行到下一步了.