1.1 返回数组索引的引用
1.2 int & operator[](int index)
1.3 return this->pAddress[index]
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
#define SIZE 10
class MyArray {
public:
MyArray(){
for (int i = 0; i < SIZE; i++) {
array[i] = i + 25;
}
}
int& operator[](int index) {
if (index > SIZE) {
cout << "索引超过最大值" << endl;
//return 0;//C2440 'return': cannot convert from 'int' to 'int &'
return array[0];
}
return array[index];
}
private:
int array[SIZE];
};
void test01() {
MyArray arr1;
cout << "arr1[3]=" << arr1[3] << endl;
arr1[3] = 49;
cout << "arr1[3]=" << arr1[3] << endl;
}
int main(){
test01();
system("pause");
return 0;
}