Following this post I implemented an accessor like
template<class T> class qv {
virtual const T& operator[](int i) const = 0;
T& operator[](int i) { return const_cast<T&>(static_cast<const qv*>(this)->operator[](i)); }
};
template<class T> class qq : public qv<T> {
public:
const T& operator[](int i) const override { return this->data[i]; }
protected:
T data[5];
};
but get an assignment of read-only location
when trying to do something like:
int main(int argc, char** argv) {
qq<int> q;
q[3] = 2; // read-only location compile error, g++ 6.3
}
It's something with the inheritance that's causing issues but I don't know what or why. As an aside does it matter if I use a static or const_cast for the inner cast above?