Subscript Operator Overloading

Apr 4, 2021 at 12:04am
Hello
I need to build a subscript operator for my array of unsigned integers. I was wondering if the subscript operator variable type needs to be "unsigned int" as the argument like the code shown down below.

1
2
unsigned int& operator [](unsigned int); 
const unsigned int& operator [] (unsigned int) const; 

Apr 4, 2021 at 12:41am
No, it doesn't need to be unsigned int. It can be whatever type you want. For example, std::map's operator overload [] can take in whatever the key type of the map is, so it could be a double, or a string, or user-defined object, etc.
Apr 7, 2021 at 6:16pm
Thank you!
Topic archived. No new replies allowed.