类名 & 函数名 这是什么意思

来源:百度知道 编辑:UC知道 时间:2024/06/22 16:53:24
class Point
{
public:
Point(int xx,int yy)
{
X=xx;
Y=yy;
cout<<"abcd"<<endl;
}
~Point()
{
cout<<"Deleting...."<<endl;
}
int GetX() {return X;}
int GetY() {return Y;}
void Move(int x,int y)
{
X=x;
Y=y;
}
private:
int X,Y;
};
class ArrayOfPoints
{
public:
ArrayOfPoints(int n);
{
numberOfPoints=n;points=new Point[n];
}
~ArrayOfPoints()
{
cout<<"Deleting..."<<endl;
numberOfPoints=0;delete [] points;
}
Point & Element(int n)
{
return points[n];
}
private:
Point * points;
int numberOfPoints;
};
int main()
{
int number;
cin>>number;
ArrayOfPoints points(number);
points.Element(0).Move(5,10);//再问下这怎么可以这样访问呢?
points.Element(1).Move(1

Point & Element(int n)这个是函数,其中Element是函数名,Point &表示这个函数的返回值是Point引用

这个问题清楚了,下面的也就明白了,points.Element(0)返回一个Point对象的引用,当然可以调用move函数了