I'm looking at the rapidjson code for possible integration. I can see that thanks to the new C++11 you can in fact do associative arrays in C++, although I'm not sure the speed advantages. However, in their example code I'm seeing this:
Document document; // Default template parameter uses UTF8 and MemoryPoolAllocator.
char buffer[sizeof(json)];
memcpy(buffer, json, sizeof(json));
if (document.ParseInsitu(buffer).HasParseError())
return 1;
printf("\nAccess values in document:\n");
assert(document.IsObject()); // Document is a JSON value represents the root of DOM. Root can be either an object or array.
assert(document.HasMember("hello"));
assert(document["hello"].IsString());
printf("hello = %s\n", document["hello"].GetString());
It looks like Document is a class that has methods being called, but at the same time he's able to access it using document["hello"] as an associative array? Is that what's going on here?
In C++, operator[]
can be overloaded by a class. Document
must either have implemented an overload or derived from one that has.
The syntax is roughly:
class Foo {
public:
AssociatedType operator [] (IndexingType i) {
//...
}
};
The AssociatedType
may be a reference. The method may be const
.