English 中文(简体)
计算从物体开始的距离时,_ATL_PACKING常数有什么用?
原标题:What s the use of _ATL_PACKING constant when computing distance from the start of object?

ATL功能一组用于所谓COM映射的宏。COM映射是一个表,它将接口GUID与偏移量相关联,该偏移量将添加到这个指针以获取相应的子对象-整个内容可以替换显式static_cast,用于IUnknown::QueryInterface()内部的上行。

映射条目是通过使用<code>offsetofclass</code>宏构建的:

#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?

最佳回答

存在非零常量是因为宏不使用空指针。我们知道,空指针的值是一个空指针常量,其计算结果为0

C++标准4.10/1指针转换[conv.ptr]:

A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of pointer to object or pointer to function type....

这是关于从派生类转换为基类指针类型的相关子句:

C++标准4.10/3指针转换[conv.ptr]:

An rvalue of type “pointer to cv D,” where D is a class type, can be converted to an rvalue of type “pointer to cv B,” where B is a base class (clause 10) of D. If B is an inaccessible (clause 11) or ambiguous (10.2) base class of D, a program that necessitates this conversion is ill-formed. The result of the conversion is a pointer to the base class sub-object of the derived class object. The null pointer value is converted to the null pointer value of the destination type.

基本上,空指针防止指针算术在派生到基的转换过程中启动;你只会得到另一个空指针。该算法用于在此类转换过程中“修复”非空指针,使其指向正确的子对象。offsetofclass宏依赖于此算法来确定偏移量。

使用的数字8是任意的。你可以在那里使用任何数字,比如14,只要它不是零。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签