为什么编译的结果不是想要的 啊?

东东同学 2012-07-29 04:39:20
#include<iostream>
using namespace std;

class Point{
public:
Point():x(0),y(0){
cout<<"Default Construction called."<<endl;
}
Point(int x,int y):x(x),y(y){
cout<<"Constructor called."<<endl;
}
~Point(){cout<<"Destructor called."<<endl;}
int getX()const{return x;}
int getY()const{return y;}
void move(int newX,int newY){
x=newX;
y=newY;
}
private:
int x,y;
};

class ArrayOfPoints{
public:
ArrayOfPoints(int size):size(size){
points=new Point[size];
}
~ArrayOfPoints(){
cout<<"Deleting······"<<endl;
delete[] points;
}
Point &element(int index){
assert(index>=0&&index<size);
return points[index];
}
private:
Point* points;
int size;
};



int main()
{
int count;
cout<<"Please enter the count of points:";
cin>>count;
ArrayOfPoints point(count);
point.element(0).move(5,0);
point.element(1).move(15,20);
system("pause");
return 0;
}

结果本应该是:
Please enter the count of points:2
Default Construction called.
Default Construction called.
Deleting······
Destructor called.
Destructor called.
然而我用DevC++和VS2008 编译的结果:
Please enter the count of points:2
Default Construction called.
Default Construction called.
各位帮忙看看!!!!!!!!!!
...全文
74 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingxuke 2012-07-31
  • 打赏
  • 举报
回复
解释楼主的疑问,主要涉及到 对象的作用域 和 对象的生命周期 的概念。

一个对象什么时候销毁(什么时候对象的生命周期结束)呢?
1,超出了对象的作用域(隐式销毁)
2,程序运行结束(隐式销毁)
3,程序员显式删除或释放(显式销毁)

当然对象在销毁时会调用自己的析构函数来进行一些需要处理操作,如释放占用的内存或其它功能性操作。

楼主的第一段程序之所以没有出现预期的结果,就是因为对象并没有销毁,所以析构函数没有得到调用。
楼上的几位已经给出了解决方法:
-1楼的方法主要通过给对象设定作用域(加大括号)来设定对象的生命周期;
-2楼的方法主要是让程序可以运行结束,从而使对象的生命周期结束;
-当然楼主也可以显示的调用对象的析构函数来显示的销毁对象。

理解了上面的概念,相信楼主很容易明白为什么第二段程序会有预期的结果:因为楼主对对象进行了显式的释—-通过使用delete操作符显式地销毁了相应地对象。

顺便说一下,第一段程序的对象是栈内存分配方式,不能通过delete操作符进行显式释放(楼主应该懂的)。

楼主,Good Luck!
东东同学 2012-07-30
  • 打赏
  • 举报
回复
一楼的方法很对我的胃口,不用非得改成VS运行,用Dev就可以。2楼的方法也很好,我的问题应经解决啦,谢谢大家!
大家看看这个为什么可以就可以呢?

#include<iostream>
using namespace std;

class Point{
public:
Point():x(0),y(0){
cout<<"Default Construction called."<<endl;
}
Point(int x,int y):x(x),y(y){
cout<<"Constructor called."<<endl;
}
~Point(){cout<<"Destructor called."<<endl;}
int getX()const{return x;}
int getY()const{return y;}
void move(int newX,int newY){
x=newX;
y=newY;
}
private:
int x,y;
};

int main()
{
cout<<"Step one: "<<endl;
Point *ptr1=new Point;
delete ptr1;

cout<<"Step two: "<<endl;
ptr1=new Point(1,2);
delete ptr1;
system("pause");
return 0;
}


SdCoder 2012-07-29
  • 打赏
  • 举报
回复
楼主用两个编译器执行时,点两下回车看看情况。。。

我用C_Free测试是这样的:
Please enter the count of points:2
Default Construction called.
Default Construction called.
请按任意键继续. . .(☆:此处点了一点回车,下面的才显示的)
Deleting······
Destructor called.
Destructor called.
请按任意键继续. . .

把你的 system("pause");去掉后,显示的是:
Please enter the count of points:2
Default Construction called.
Default Construction called.
Deleting······
Destructor called.
Destructor called.

由于system("pause"),因为程序还没有结束,编译器不确定你是否还用不用这两个对象,所以编译器不会自动释放掉这两个对象,也就不会执行那段析构函数了,再点回车后,表示退出程序,所以编译器自动释放两个对象的内存,两个析构函数也就执行了。
pathuang68 2012-07-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

运行到pause的地方,还没到大括号},
所以对象还没释放。

加个大括号,将puase之上的代码括起来就行了。

C/C++ code

int main()
{
{
int count;
cout<<"Please enter the count of points:";
cin>>count;
ArrayOfPoints point(coun……
[/Quote]

这种方式也可以,不过我还是建议用2楼建议的方式。
pathuang68 2012-07-29
  • 打赏
  • 举报
回复
你的代码在VS2010上运行是正常的。

问题在于你加了一个system("pause");所以出现:
Please enter the count of points:2
Default Construction called.
Default Construction called.
程序就暂停在那里,你再回车就会显示后面的:
Deleting······
Destructor called.
Destructor called.
但就在这瞬间,窗口就关闭了,以至于你认为后面的三行根本没有出现。

建议:将system("pause");删除,然后用下面的方式对项目进行设置:
xx项目属性->配置属性(Configuration Properties)->链接(Linker)->系统(System)->然后将右边第一行的SubSystem的内容选为:Console(/SUBSYSTEM:CONSOLE),完成后保存设置。然后按CTRL+F5运行你的项目。

这样就会得到你想要的结果了。

由于这样的原因,我不太推荐用system("pause");这种方式来暂停运行,而推荐修改项目属性配置的方式。
陈厚来 2012-07-29
  • 打赏
  • 举报
回复
运行到pause的地方,还没到大括号},
所以对象还没释放。

加个大括号,将puase之上的代码括起来就行了。


int main()
{
{
int count;
cout<<"Please enter the count of points:";
cin>>count;
ArrayOfPoints point(count);
point.element(0).move(5,0);
point.element(1).move(15,20);
}

system("pause");
return 0;
}


64,654

社区成员

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

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