I am reading Essential COM and encountered a macro ‘BASE_OFFSET’ from chapter 2 of the book and I don’t really understand its syntax or why it’s done that way.
#define BASE_OFFSET(ClassName, BaseName) \
(DWORD_PTR(static_cast<BaseName*>(reinterpret_cast<ClassName*>(0x10000000))) - 0x10000000)
Can anyone explain this macro and how we use this? In fact, the book uses this macro but since I don’t really understand it, I don’t see the practical usage of it. Thank you very much in advance.
The macro makes up a dummy pointer to
ClassNamewith thereinterpret_castand then casts it to theBaseNamewith thestatic_cast.In the presence of multiple inheritance, the address of a base class subobject is not always the same as the address of the object. This possibly-different-address is subtracted from the original dummy address, to obtain the offset of the
BaseNamesubobject in aClassNameobject. It’s similar tooffsetof, but for base class subobjects instead of members.This is only useful if you’re doing some nasty low-level stuff.