I would like to create a C++ object to wrap the RAM of an external peripheral device. I'm trying to set up something like the following:
Peripheral p;
p[4] = 10;
int n = p[5];
To do this I need to read or write to the peripheral whenever an array element is accessed. I cannot work out how to do this using operator overloading etc. I can return an "accessor" object that can be used as an lvalue in the second line:
PeripheralAccessor Peripheral::operator[](int i);
or I can define a "simple" operator that can be used to read an int from the peripheral on the third line:
int Peripheral::operator[](int i);
but I cannot get the two to coexist to give both read and write access to the peripheral. I can define this second operator as a const (rvalue) operator, but it will ONLY then be called for a const instance of the class, which isn't enough for my needs...
Hopefully I've explained what I'm trying to achieve here clearly; can anybody suggest how I should be doing it (or indeed whether it is possible)?