注册 登录
编程论坛 C++教室

求助~~Point&Element(int n)什么意思?

夜de轨迹 发布于 2009-10-28 11:44, 736 次点击
谁能解释下最后的points.Element(0).Move(5,10)是怎么引用的,Point&Element(int n)什么意思?

#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<<"constructor 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 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;
    cout<<"Please enter the number of points:";
    cin>>number;
    Arrayofpoints points(number);
    points.Element(0).Move(5,10);
    points.Element(1).Move(15,20);
}
3 回复
#2
夜de轨迹2009-10-28 11:56
Point&Element(int n)
{ return points[n];}
高手帮忙解释下这句..我没找到这种语法形式..在线等着..
#3
qlc002009-10-28 16:22
Point&Element(int n)中的points相当于类Point的指针数组,而Element()定义为Point类型的函数,所以points可以调用Point类中的函数Move(),而Arrayofpoints points(number);中的points声明为Arrayofpoints这个类的对象,所以可以使用points.Element(0).Move(5,10)这种形式。
#4
pywepe2009-10-28 16:34
中间应该有一个空格
返回point的引用
1