为什么编译的结果不是想要的 啊?
东东同学 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.
各位帮忙看看!!!!!!!!!!