C++问题Point& Element(int n) { return points[n]; }
来源:互联网 发布:淘宝上怎么退货 编辑:程序博客网 时间:2024/06/05 09:24
C++问题Point& Element(int n) { return points[n]; }
#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中的值。同时操作结束后不会调用析构函数。
- C++问题Point& Element(int n) { return points[n]; }
- Point &element(int index){ return points [index]; }
- Point &element(int index){ return points [index]; }
- return n和exit(int n)小结
- int *n;int &n;int *&n
- stat(int a[],int n,int c[][2])
- 【C++】n皇后问题
- C n钱百鸡问题
- setw(n),setfill(char c),setbase(int n)函数练习
- int *p[n]; int (*p)[n];
- int n与int... n的区别
- C/C++ N皇后问题
- N皇后问题 c/c++
- n皇后问题--c代码
- 编写函数int stat(int a[],int n,int c[][2])。
- 编写函数int stat(int a[],int n,int c[][2])
- 编写函数int stat(int a[],int n,int c[][2])
- N*N皇后问题
- Spring中使用注解的方式注入方式1
- 最长递增子序列
- 关于Django model 与json序列的转换
- jdk安装
- AIX复习笔记一
- C++问题Point& Element(int n) { return points[n]; }
- Android 通用json解析方法
- PHP连接 SQLSERVER 注意事项(经典中的经典)
- .NET Web API之filter ActionFilterAttribute 过滤器使用
- 2013第三届蓝桥杯C/C++本科预赛1 高斯日记
- 程序员面试攻略题2
- JS解析json数据(如何将json字符串转化为数组)
- Project Anarchy 概要
- poj3009-Curling2.0