I’ve noticed things like what follows in C++.
SomeClass obj = SomeClass();
int boo = obj["foo"];
What is this called and how can I do it?
example
class Boo {
public:
int GetValue (string item) {
switch (item) {
case "foo" : return 1;
case "apple" : return 2;
}
return 0;
}
}
Boo boo = Boo();
int foo = boo.GetValue("foo");
// instead of that I want to be able to do
int foo = boo["foo"];
To use
[], you’d overloadoperator[]:You might be interested to know
std::mapalready provides pretty much what you seem to be looking for:The obvious difference is that when/if you use this to look up a value that hasn’t previously been inserted, it’ll insert a new item with that key and the value 0.