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 ClassName
with the reinterpret_cast
and then casts it to the BaseName
with the static_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 BaseName
subobject in a ClassName
object. It's similar to offsetof
, but for base class subobjects instead of members.
This is only useful if you're doing some nasty low-level stuff.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With