I am wondering if there is a way to overload operator[] for a non-class type in C++.
Basically, there is a data type which is a pointer (CFDictionaryRef from CoreFoundation). But it’s not a class (i know that overloading operator[] for a specific class is allowed). I know how to access each element inside the CFDictionaryRef (for example, by using CFDictionaryGetIndex(CFIndex index); ). I want to make it simplified so that I don’t have to write that function call every time. I want to overload the operator[ ] for CFDictionaryRef. But since it’s not a class, from what I see, it’s not possible.
Anyone got any suggestions?
You’re right, it’s not possible to overload operators on non-user-defined types.
What you might do is wrap the pointer in a class and overload the operators on the class itself. Since you can only overload operators on class types, this is the only option.
This also has the advantage of being able to automatically manage the lifetime of the pointer (RAII) if you need to do that.