读书人

关于动态数组的一个小疑点

发布时间: 2012-04-05 12:42:39 作者: rapoo

关于动态数组的一个小问题
#include <iostream>
using namespace std;
class Point
{
public:
Point()
{X=Y=0;cout < < "Default Constructor Called. " < <endl;}
Point(int xx,int yy)
{X=xx;Y=yy;cout < < "Constructou called. " < <endl;}
~Point()
{cout < < "Destructor called. " < <endl;}
int GetX(){return X;}
int GetY(){return Y;}
void Move(int x,int y)
{X=x;Y=y;}
private:
int X,Y;
};

class ArrayOfPoint
{
public:
ArrayOfPoint(int n)
{numberOfPoint=n;points=new Point[n];}
~ArrayOfPoint()
{cout < < "Deleting... " < <endl;
numberOfPoint=0;delete[] points;
}
Point& Element(int n)
{return points[n];}
private:
Point *points;
int numberOfPoint;
};
int main() //主程序略
{
.
.
.
.
.
}


Point& Element(int n)
{return points[n];} //这里为什么要加& 返回的是什么类型的??

[解决办法]
Point& Element(int n)
{return points[n];} //这里为什么要加& 返回的是什么类型的??
===>
返回n位置Point的引用,
这样就可以这样用了:
Point p(10,20);
ArrayOfPoint arrP(10);
arrP[0]=p;
arrp[7]=p;
.............

读书人网 >C++

热点推荐