【发布时间】:2011-06-28 04:41:59
【问题描述】:
ATL 功能 a set of macros for so-called COM maps。 COM 映射是一个表,它将接口 GUID 与要添加到 this 指针以获取相应子对象的偏移量相关联 - 整个东西作为 explicit static_cast for the upcast inside IUnknown::QueryInterface() 的替代品。
地图条目是使用offsetofclass宏构建的:
#define _ATL_PACKING 8
#define offsetofclass(base, derived)\
((DWORD_PTR)(static_cast<base*>((derived*)_ATL_PACKING))-_ATL_PACKING)
为了便于阅读,我将其重写为以下伪代码“函数”:
derived* derivedPointer = (derived*)_ATL_PACKING;
base* basePointer = static_cast<base*>(derivedPointer);
DWORD_PTR offset = (DWORD_PTR)(basePointer)-_ATL_PACKING;
看起来很合理 - 它获得一个指向虚构派生对象的指针,然后执行显式 static_cast 来移动指针,然后计算这些虚构对象之间的距离。
问题是为什么常数 8 在那里?为什么我们需要这个常数,为什么选择 8?
【问题讨论】:
标签: c++ com macros casting atl