#include<iostream>
using namespace std;
class Point
{
public:
private:
};
class ArrayOfPoints
{
public:
private:
};
int main()
{
}
其中的Point& Element(intn) 是什么意思呢?解释如下
这里的Point是返回类型,Point&是按地址传送返回值。
如果把&去掉变成
Point Element(int n)
再加上语句
Point p=points.Element(0);
p.Move(5,10);
那么p就是一个新建的对象,系统将points中单元0中存放的值复制给p,p是points的单元0的一个副本。你对p所做的操作不会影响points的单元0中的值。当操作完成时p会被析构。
把&再加回去:
Point& Element(int n)
Point p=points.Element(0);
p.Move(5,10);
系统将Element(0)的返回值按地址赋给p,p的地址就是points的单元0的地址,p是points的单元0的一个别名。你对p所做的操作就是直接对points的单元0操作,会影响points的单元0中的值。同时操作结束后不会调用析构函数。