C++const修饰的函数问题
为什么Array& operator[] (const int& index) const改为Array& operator[] (const int& index)就编译通过,而Array operator[] (const int& index)const用const来修饰,编译时却不会出问题?? 求高手指教
编译出错提示:
error C2440: 'return' : cannot convert from 'const struct Array' to 'struct Array &'
#include <iostream>
using namespace std;
struct Array
{
int a;
};
class OpeArr
{
public:
Array arr[10];
public:
OpeArr()
{
for (int i = 0; i < 10; i++)
{
arr[i].a = i;
}
}
Array operator[] (const int& index)const
{
return arr[index];
}
Array& operator[] (const int& index) const
{
return arr[index];
}
};
int main()
{
OpeArr test;
cout << test.arr[2].a << endl;
return 0;
}