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?
The operator[]
declared in derived class hides the one in the base class. So qv<T>::operator[]
can't be found in name lookup.
(emphasis mine)
name lookup examines the scopes as described below, until it finds at least one declaration of any kind, at which time the lookup stops and no further scopes are examined.
You could add using qv<T>::operator[];
to introduce the name operator[]
into the derived class. e.g.
template<class T> class qq : public qv<T> {
public:
using qv<T>::operator[];
const T& operator[](int i) const override { return this->data[i]; }
protected:
T data[5];
};
BTW: I suppose that it's a typo to declare qv<T>::operator[]
as private
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With