Point &element(int index){ return points [index]; }

#include<iostream>

using namespace std;

class Point

{

public:

Point()

{

X=Y=0;

cout<<"默认的构造函数被调用..."<<endl;

}

Point(intx,int y)

        {
                X=x;
                Y=y;
                cout<<"xy的构造函数被调用..."<<endl;
        }
        GetX()
        {
                cout<<X<<endl;
        }
        GetY()
        {
                cout<<Y<<endl;
        }
        void Move(int x,int y)
        {
                X=x;
                Y=y;
                cout<<X<<Y<<endl;
        }
private:
        int X,Y;
};
class ArrayOfPoint
{
public:
        ArrayOfPoint(int n)
        {
                numberOfPoints=n;
                points=new Point[n];
        }
        ~ArrayOfPoint()
        {
                numberOfPoints=0;
                delete[] points;
                cout<<"析构结束..."<<endl;
        }
        Point& Element(int n)
        {
                return points[n];
        }
        
private:
        int numberOfPoints;
        Point *points;
};
int main()
{
        int num;
        cout<<"请输入要动态创建的个数:"<<endl;
        cin>>num;
        ArrayOfPoint Point(num);
        Point.Element(0).Move(5,4);
        Point.Element(1).Move(65,99);
        return 0;
}



解释一下Point& Element(int n)  



这里的Point是返回类型,Point&是按地址传送返回值。
如果把&去掉变成
Point Element(int n) 
再加上语句
Point p=points.Element(0);
p.Move(5,10);
那么p就是一个新建的对象,系统将points中单元0中存放的值复制给pppoints的单元0的一个副本。你对p所做的操作不会影响points的单元0中的值。当操作完成时p会被析构。
&再加回去:
Point& Element(int n)
Point p=points.Element(0);
p.Move(5,10);
系统将Element(0)的返回值按地址赋给pp的地址就是points的单元0的地址,ppoints的单元0的一个别名。你对p所做的操作就是直接对points的单元0操作,会影响points的单元0中的值。同时操作结束后不会调用析构函数


&是返回一个变量的引用。简单地说加了&返回的是points[index]的引用,就能对其赋值;不加&,就只是一个返回值,不能对points[index]赋值。



C++point&element(int size)pointelement(int size)有什么区别

前者是声明对象的引用,后者是声明一个对象。引用是一个对象的别名,主要用于函数参数和返回值。说直白点,引用就是一个内存的别名。所以两两者在作为函数参数传递时不一样,引用传递的是地址,而后者传递的是值。
加了引用的好处好处最大的一个是返回值可以成为一个左值
假设这个函数在类C中,则你可以这样写
C a;
C b;
c.element(size1) = b.element(size2) = b.elemnt(size3)
这样可以方便的改写某个类的某个成员。至少这样的写法暗示了这个类需要这样的功能。

因为我们创建的points对象是一个在类内部由动态数组实现的对象,而我们对数组的操作无非就是取元素,改变元素,如果element不返回一个引用时,那么我们可以认为这个元素是只读的,我们无法通过赋值等途径改变这个元素(其实是一个对象)的值,而我们返回一个引用的话,就相当于取出元素本身,就可以对它进行正常的读写操作。这样才符合数组的性质。







 
整理网上资料所得。。。。。。谢谢。




  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码We now want to always redraw all the points that have ever been drawn in the panel, not just the last point. To do this, we must save the coordinates of all these points so that we can redraw them all one by one in the paintComponent method every time this method is called. To save the coordinates of the various mouse positions we click, replace the x and y instance variables of the MyPanel class with a single private instance variable called points of type ArrayList<Point>. The Point class is provided to you by Swing. In the constructor of MyPanel, initialize the points instance variable with a new arraylist object of the same type. In the mouseClicked method of the mouse listener, use the getPoint method of the mouse event object to get a Point object representing the position of the mouse click (that Point object internally stores both the x and y coordinates of the mouse click event). Then add this Point object to the arraylist using the arraylist’s add method. Then, in the paintComponent method, add a loop to draw in the panel all the points of the arraylist. You can get the number of elements in the arraylist by using the size method of the arraylist; you can access a specific element of the arraylist at index i by using the get(i) method of the arraylist (element indexes start at zero in an arraylist). The Point class has getX and getY methods to get the coordinates of the point (these two methods return values of type double so you need to cast the returned values into the int type before you can use them to draw a point).
最新发布
05-05

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值