I am learning about C++ in my spare time, and I am implementing a class that overloads the subscript / []
operators. Now, I wanted to be able to pass two or more arguments to the subscript operators as follows (separated by a comma):
myObject[i,j,...]
, where the arguments could for instance be string
s or int
s
I found out that C++ does not allow the subscript operator to accept more than one argument. However, I read about overloading the comma operator in this thread, as an alternative solution to that 'problem'.
In the example, the comma operator is overloaded using very specific signature, with two custom Enum
's, but I can see that it's not a good idea to overload the comma operator for general type pairs (which would be my use case).
With my limited knowledge of C++, that made me curious about whether it would be at all possible to limit the 'scope' or use of the comma operator overload to a specific situation or context, e.g, only in the subscript operator overload definition, giving me the functionality I am interested in, without causing interference elsewhere.