首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-04-05 
关于动态数组的一个小问题#include iostreamusingnamespacestdclassPoint{public:Point(){XY0cout

关于动态数组的一个小问题
#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;
.............

热点排行