My first time overloading the [] operator for something practical and I ran into something I never thought of before.
I’m trying to make a custom array-like class that holds pointers (and offers some unique features).
Returning one of the pointers from my class by using the [] operator is obvious, but I was also envisioning the ability to change the target address of the pointer from the outside.
Maybe I’m over-thinking this but wouldn’t an assignment onto the return value (like fish[0]=lpHatAddress;) cause an error because the value being returned is just an address?
The only solution I’ve been able to think of is storing pointers to pointers, and that would take twice as much memory.
Am I overlooking something obvious?
Is there a clean way to do this?
You should have the result of the operator be a reference. So if your data type is
int, the operator returns anint&.