I implemented SpareArray
class. It's a big one, so there is no sense to show it all, but what is important is that is has insert
method, which makes it possible to insert values at any index (from zero to "infinity"). So, my client code may look like so:
auto arr = new SpareArray<int>{};
arr.insert(100, 1);
The above code inserts value 1
at index 100
. Now, I want to be able to use square brakets notation to get the same result:
auto arr = new SpareArray<int>{};
arr[100] = 1; //I want this line to internally call arr.insert(100, 1);
So, how can operator[]
be difined to internally call insert
method? I need this call, because insert
method has some business logic and I want this business logic to take place also in this case (in case of []
).