I’m writing a collection class. I want to overload the square brackets operator ([]) to provide access to elements in the collection.
int operator[](int i)
{
// Do stuff here
}
My problem is that I don’t see how to write this so that I could use this operator to accept a value:
myClassInstance[0] = value;
I see no way to declare the square brackets operator with an additional argument (the value to assign to the element).
I know I can simply return int& and the caller can assign a value to that, but internally each element is stored in a different format than the one made public.
Is this even possible?
Write an
int_proxyclass that is implicitly convertible tointand is assignable fromint. You’ll need at least two member functions:In this proxy class, store whatever information you need to be able to retrieve and set the value in the container. Do the retrieval in the conversion operator and the assignment in the assignment operator.