Help me explain this directive
-
Found this in MSDN "The COM Programmer's Cookbook". It is a #define to find an instance pointer from an interface in vanilla C.
#define IMPL(class, member, pointer) \
(&((class *)0)->member == pointer, ((class *) (((long) pointer) - offsetof (class, member))))What I have some difficulties understanding is the cast
(&((class *)0)
. What exactly does that do here? :confused: Thanks /moliate -
Found this in MSDN "The COM Programmer's Cookbook". It is a #define to find an instance pointer from an interface in vanilla C.
#define IMPL(class, member, pointer) \
(&((class *)0)->member == pointer, ((class *) (((long) pointer) - offsetof (class, member))))What I have some difficulties understanding is the cast
(&((class *)0)
. What exactly does that do here? :confused: Thanks /moliateIt casts the pointer returned to be the type that is passed in by the class variable, because it is first cast to long for a subtraction. Presumably it is that type of pointer to start with. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
-
It casts the pointer returned to be the type that is passed in by the class variable, because it is first cast to long for a subtraction. Presumably it is that type of pointer to start with. Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
I´m feeling really stupid here, but I still don´t get it. The problem is still the first comma expression. If the interface is the first in a struct, would it not compile down to
0
, leaving a nasty null pointer exception? And what will the result be for the==
operation ifclass->member
does not equalpointer
? And isn't the pointer returned the one in the second comma expression? I really want to understand this, but I have only used commas in simple for-loops before... /moliate -
I´m feeling really stupid here, but I still don´t get it. The problem is still the first comma expression. If the interface is the first in a struct, would it not compile down to
0
, leaving a nasty null pointer exception? And what will the result be for the==
operation ifclass->member
does not equalpointer
? And isn't the pointer returned the one in the second comma expression? I really want to understand this, but I have only used commas in simple for-loops before... /moliate#define IMPL(class, member, pointer) \ (&((class *)0)->member == pointer, ((class *) (((long) pointer) - offsetof (class, member)))) I saw the first bit the first time around and didn't think about it much, but it certainly seems to me that it's trying to dereference a NULL pointer and access a member of it, ( before checking if the address of the member == the pointer ), which surely should crash ? Where is this used ? Where does it come from ? Christian The tragedy of cyberspace - that so much can travel so far, and yet mean so little. "I'm somewhat suspicious of STL though. My (test,experimental) program worked first time. Whats that all about??!?! - Jon Hulatt, 22/3/2002
-
Found this in MSDN "The COM Programmer's Cookbook". It is a #define to find an instance pointer from an interface in vanilla C.
#define IMPL(class, member, pointer) \
(&((class *)0)->member == pointer, ((class *) (((long) pointer) - offsetof (class, member))))What I have some difficulties understanding is the cast
(&((class *)0)
. What exactly does that do here? :confused: Thanks /moliateBasically it is a manipulation in "C" in order to get what we have in C++ where classes are concerned. It looks like
IMPL
is a way to get the this pointer from an interface pointer.(&((class *)0)->member == pointer
The NULL pointer is first converted to pointer that is the desired type, and this pointer is compared to the input pointer. If this comparison was not between two pointers that had a typesafe cast, the compiler would throw an error at this point in the compilation. (&((class *)0)->member == pointer :),:) ((class *) (((long) pointer) - offsetof (class, member)))) The key to this macro, is the comma after the comparison test, this basically throws away the result of the comparison, the compiler will see this, and consequently throw away this comparison test in the final compiled code. Then the rest of the macro is used to calculate the offset to the instance data for the struct. So in short, that comparison is a typesafe cast check in "C", that is performed competely at compile time. Hope this helps!
Checkout my Guide to Win32 Paint for Intermediates
-
Basically it is a manipulation in "C" in order to get what we have in C++ where classes are concerned. It looks like
IMPL
is a way to get the this pointer from an interface pointer.(&((class *)0)->member == pointer
The NULL pointer is first converted to pointer that is the desired type, and this pointer is compared to the input pointer. If this comparison was not between two pointers that had a typesafe cast, the compiler would throw an error at this point in the compilation. (&((class *)0)->member == pointer :),:) ((class *) (((long) pointer) - offsetof (class, member)))) The key to this macro, is the comma after the comparison test, this basically throws away the result of the comparison, the compiler will see this, and consequently throw away this comparison test in the final compiled code. Then the rest of the macro is used to calculate the offset to the instance data for the struct. So in short, that comparison is a typesafe cast check in "C", that is performed competely at compile time. Hope this helps!
Checkout my Guide to Win32 Paint for Intermediates