这段代码是在c++中访问类中的关联数组吗?
Is this code accessing an associative array in a class in C++?
我正在查看rapidjson代码以进行可能的集成。我可以看到,由于新的c++ 11,您实际上可以在c++中执行关联数组,尽管我不确定速度优势。然而,在他们的示例代码中,我看到这样:
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 = %sn", document["hello"].GetString());
它看起来像文档是一个类,有方法被调用,但同时他能够访问它使用文档["hello"]作为一个关联数组?这就是这里发生的事吗?
在c++中,operator[]
可以被类重载。Document
必须要么实现了重载,要么从重载派生而来。
语法大致如下:
class Foo {
public:
AssociatedType operator [] (IndexingType i) {
//...
}
};
AssociatedType
可能是参考。方法可以是const
操作符重载在c++早期就已经可用了。
在RapidJSON中,有几个重载operator[]
在GenericValue
中定义,如:
template<typename Encoding, typename Allocator>
template<typename T >
GenericValue& rapidjson::GenericValue< Encoding, Allocator >::operator[](T* name)
GenericDocument
是由GenericValue
衍生而来
相关文章:
- 在 C++ 中访问数组负索引处的内存不会返回垃圾
- 使用指针访问数组中的对象数据成员
- VTK 图像数据,访问数组中的 1 个图像
- 调用函数后无法访问数组
- 如何从地图访问数组的元素?
- 访问数组中的不同元素
- 使用指针访问数组元素时出现意外结果
- 使用指针访问数组的值
- C++基于1而不是0的访问数组,方法是在编译时移位
- 如何在主函数之外访问数组?
- 如何在 llvm 的 cpp api 中使用 CreateInBoundsGEP 来访问数组的元素?
- 访问数组中所有结构的成员
- C 是否每次访问数组时都复制元素?
- C++ 为什么访问数组中的元素会返回指针?
- 如何通过指针访问数组的元素
- 指针数组交换无需直接访问数组
- 直接从指针/地址访问数组的元素
- 我有一个 Qt 对象的 2d 数组,我已经用值播种了这些对象。如何访问数组中特定对象的值并更改它们?
- 如何使用 C+ 中的指针访问数组中的特定索引
- 通过两个下标访问数组成员