Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASE_OFFSET macro from Essential COM

Tags:

c++

macros

com

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.

like image 395
istudy0 Avatar asked Dec 10 '11 16:12

istudy0


1 Answers

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.

Diagram showing an example

This is only useful if you're doing some nasty low-level stuff.

like image 183
R. Martinho Fernandes Avatar answered Nov 01 '22 00:11

R. Martinho Fernandes