关于析构的时间问题???

ljx1678 2009-04-07 08:24:34
我想显性的表示出对象何时被析构,但是在VC6。0上根本没有办法这样。而在VC2008上的结果也无法解释。大家帮我看看呀。
下面是代码:
#if!defined _POINT_H
#include <iostream>
#include <math.h>

using namespace std;

class Point{
private:double x,y;

public:Point(){}
~Point();
void set();
void display();
static double lenth(const Point & ,const Point &);
};

void Point::set ()
{
int a,b;
char c;
cout << "please input x and y:";
cin >> a ;
c=getchar();
cin >> b ;
x=a;
y=b;
}

Point::~Point ()
{
cout << "P(" << x <<","<<y<< ")" << "has been deleted." << endl;
}
void Point::display()
{
cout << "(" << x <<"," << y << ")" << " ";
}

double Point::lenth(const Point & P1,const Point & P2 )
{
double lenth;
lenth=pow((pow(P1.x-P2.x,2)+pow(P1.y-P2.y,2)),0.5);
return lenth;
}

#endif




#include <iostream>
#include "point.h"


using namespace std;

int main()
{
Point* ptp;
ptp = new Point[10];
for(int i=0;i<10;i++)
{
cout << "P" << i+1 << ":";
ptp[i].set ();
}
cout << endl;
cout << "ten points:";
for(int j=0;j<10;j++)
{
ptp[j].display ();
}
cout << endl;
double lenth=0;
for(int k=0;k<9;k++)
{
lenth+=Point::lenth (ptp[k],ptp[k+1]);
}
cout << "the lenth of the lines connecting this ten points is " << lenth << endl;
return 0;
}

...全文
73 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljx1678 2009-04-07
  • 打赏
  • 举报
回复
没有delete []ptp;应该也可以把,我用VS2008是又显示出析构的,可是析构的时间好像有点问题。
zwzzwz1 2009-04-07
  • 打赏
  • 举报
回复

//Point.H
#ifndef _POINT_H
#define _POINT_H
#include <iostream>
#include <math.h>

using namespace std;

class Point{
private:double x,y;

public:Point(){}
~Point();
void set();
void display();
static double lenth(const Point & ,const Point &);
};

void Point::set ()
{
int a,b;
char c;
cout << "please input x and y:";
cin >> a ;
c=getchar();
cin >> b ;
x=a;
y=b;
}

Point::~Point ()
{
cout << "P(" << x <<"," <<y << ")" << "has been deleted." << endl;
}
void Point::display()
{
cout << "(" << x <<"," << y << ")" << " ";
}

double Point::lenth(const Point & P1,const Point & P2 )
{
double lenth;
lenth=pow((pow(P1.x-P2.x,2)+pow(P1.y-P2.y,2)),0.5);
return lenth;
}

#endif

//main.cpp
#include <iostream>
#include "Point.h"


using namespace std;

int main()
{
Point* ptp;
ptp = new Point[10];
for(int i=0;i <10;i++)
{
cout << "P" << i+1 << ":";
ptp[i].set ();
}
cout << endl;
cout << "ten points:";
for(int j=0;j <10;j++)
{
ptp[j].display ();
}
cout << endl;
double lenth=0;
for(int k=0;k <9;k++)
{
lenth+=Point::lenth (ptp[k],ptp[k+1]);
}
cout << "the lenth of the lines connecting this ten points is " << lenth << endl;
delete[] ptp; // 楼主用new忘记释放了.
return 0;
}

xqls_xqls 2009-04-07
  • 打赏
  • 举报
回复
delete []ptp;
mengde007 2009-04-07
  • 打赏
  • 举报
回复

#include <iostream>
#include <math.h>

using namespace std;

class Point
{
private:
double x,y;

public:
Point(){}
~Point();
void set();
void display();
static double lenth(const Point & ,const Point &);
};

void Point::set ()
{
int a,b;
char c;
cout << "please input x and y:";
cin >> a ;
c=getchar();
cin >> b ;
x=a;
y=b;
}

Point::~Point ()
{
cout << "P(" << x <<"," <<y << ")" << "has been deleted." << endl;
}
void Point::display()
{
cout << x <<"," << y << " ";
}

double Point::lenth(const Point & P1,const Point & P2 )
{
double lenth;
lenth=pow((pow(P1.x-P2.x,2)+pow(P1.y-P2.y,2)),0.5);
return lenth;
}


#include <iostream>
using namespace std;
int main()
{

Point* ptp;

ptp = new Point[10];

for(int i=0;i <10;i++)

{

cout << "P" << i+1 << ":";

ptp[i].set ();

}
cout << endl;

cout << "ten points:";

for(int j=0;j <10;j++)

{

ptp[j].display ();

}

cout << endl;

double lenth=0;

for(int k=0;k <9;k++)

{

lenth+=Point::lenth (ptp[k],ptp[k+1]);

}
cout << "the lenth of the lines connecting this ten points is " << lenth << endl;

delete []ptp;//楼主没有调用析构啊,显式的。
return 0;
}

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧