I’m 99% sure the answer to this is ‘no dice’, but I’ll try anyway…
I’m trying to override operator[].
I know how to override it for invocations on a given type:
struct Huh {
int x;
int operator[](float idx) { return -1; }
};
Huh h;
cout << h[123.4f] << end; // works, spits out -1
But what if I want to change the way I subscript an array of these types:
Huh h[10];
Huh h2 = h[123.4f]; // invalid!!
It appears this is a non-changeable C++ behavior. That is, the operator[] for type “Huh[10]” (or any array type) is off-limits.
Or, is there some way to define operator[] for an arrayed type?
I tried making operator[] a free function only to discover that’s disallowed in C++. Any other ideas?
No dice. You can’t overload a “pointer to an object”s methods, as a pointer is not an object with operators to be overloaded.