#ifndef _ARRAY_H_
#define _ARRAY_H_ class Array
{
private:
int mLength;
int* mSpace; public:
Array(int length);
Array(const Array& obj);
int length();
void setData(int index, int value);
int getData(int index);
~Array();
public:
int& operator[](int i);
Array& operator=(Array &a); };
#endif #include "iostream"
#include "Array.h"
using namespace std; Array::Array(int length)
{
if( length < 0 )
{
length = 0;
} mLength = length;
mSpace = new int[mLength];
} Array::Array(const Array& obj)
{
mLength = obj.mLength; mSpace = new int[mLength]; for(int i=0; i<mLength; i++)
{
mSpace[i] = obj.mSpace[i];
}
} int Array::length()
{
return mLength;
} void Array::setData(int index, int value)
{
mSpace[index] = value;
} int Array::getData(int index)
{
return mSpace[index]; //返回的只是一个值,没有内存地址,作不了左值,mSpace[index]本身是有内存地址的
} Array::~Array()
{
mLength = -1;
delete[] mSpace;
} int& Array::operator[](int i)
{
return mSpace[i]; //正常,因为mSpace[i]本身是有内存地址的,返回一个具有内存地址的数据是可以作引用的.
return this->getData(i);//这里会提示"非常量的引用初始化必须为左值, 难道this->getData(i)作不了左值?"
//解答:this->getDate(i)这个函数返回的只是一个值,this->getDate(i),只是返回65行函数的类型int,没有内存地址,可能存在于除了内存区外的任何地区,所以作不了引用.
//这里要确保this->getDate(i)有内存地址,所以this->getDate(i)的返回必须是引用.
} Array& Array::operator=(Array &a)
{
mLength = a.mLength;
mSpace = new int[mLength]; for (int i = 0; i < mLength; i++)
{
mSpace[i] = a[i];
} return *this;
}
  
  

  函数,大概只有返回一个左值引用的才可以用作左值。其它基本全为右值。

最新文章

  1. poj1129 Channel Allocation
  2. 常见Android Native崩溃及错误原因
  3. WCF服务接口多,客户端在引用时出错!报WCF The maximum nametable character count quota (16384) has been exceeded while reading XML data错误
  4. KnockOutJS步步深入
  5. mac下SSH很快被断开
  6. android 无线模式下使用ADB调试
  7. 【Android】 onSaveInstanceState()恢复数据
  8. centos下redis安装
  9. href 做导航 特效
  10. nefu 1029 字符串
  11. (转)java提高篇(二)-----理解java的三大特性之继承
  12. Selenium+Python进行web自动化测试(Demo+API)
  13. U3D学习资料收集
  14. 9.4、__del__、__doc__、__dict__、__module__、__getitem__、__setitem__、__delitem__、__str__、__repr__、__call__
  15. 微信网页JS分享,微信二次分享无缩略图问题
  16. python的函数(三)
  17. django请求的生命周期
  18. 7、Redis中对ZSet类型的操作命令
  19. leetCode题解之求二叉树最大深度
  20. RegExp正则匹配模式汇总

热门文章

  1. MyBatis代码自动生成
  2. Provisioning Profile
  3. C#中数组、ArrayList和List三者的区别
  4. 磁盘空间已满导致rabbitmq无法启动
  5. [BS-22] Objective-C中nil、Nil、NULL、NSNull的区别
  6. Android实现app长时间未操作时自动退出app
  7. android网络编程--从网络下载图片,并保存到内存卡
  8. Windows Server 2008 R2 域控修改域用户密码复杂性
  9. javascript设计模式学习之十二——享元模式
  10. 【未解决】CImage::Save / Load 导致“线程 0xc224 已退出,返回值为 1 (0x1)”