Hello,
I have written a class A with the access operator[](int) overloaded by
a
A-const version which returns an int by value:
------------------------------------------------
#include <iostream>
using namespace std;
class A {
public:
A() {}
int operator [] ( int i ) const {
cout << "int operator[]() called" << endl;
return vec[i];
}
int & operator [] ( int i ) {
cout << "int & operator[]() called" << endl;
return vec[i];
}
private:
int vec[10];
};
------------------------------------------------
However, the compiler uses the non const version even if only an
int-value is read:
------------------------------------------------
A aObj;
int a;
a = aObj[5]; //int & operator []() is used
------------------------------------------------
Is it possible to recognize if the compiler can use the const-version?
I'd like to write a sparse data type, where the []-operator is used in
a
cascaded way, e.g. aObj[5][6][7]. If it was possible to recognize that
a
value is only read rather than written, the use of an access operator
could return null immediatedly in the case of a missed index.
Claudius