View Single Post
Old 2nd January 2020, 00:12   #4  |  Link
LoRd_MuldeR
Software Developer
 
LoRd_MuldeR's Avatar
 
Join Date: Jun 2005
Location: Last House on Slunk Street
Posts: 13,248
Quote:
Originally Posted by stax76 View Post
That sounded weird as I thought that after casting to different interfaces it will be a different address for each interface, just tried it in the debugger and it is as you say always the same address.
Keep in mind that in C/C++ pointers are just numbers – the memory address where a certain object is located.

Consequently, doing a simple type-cast on a pointer type does not change the value of the pointer (memory address) at all! All a type-cast from type X* to type Y* really does is, it tells the compiler to assume that the object located at that memory address is of type Y rather than of type X – which can make sense or not. The C/C++ compiler does what you asked for, even if it results in undefined behavior.

On the other hand, which pointer (memory address) is returned by the QueryInterface() function totally depends on the implementation of that function! It is possible that QueryInterface(__uuidof(Y), ...) is implemented simply as "(Y*)this", in which case it would be equivalent to a simple type-cast – but you really can (should) not rely on that. After all, it is just as well possible that p->QueryInterface() returns a pointer (memory address) different from p.
__________________
Go to https://standforukraine.com/ to find legitimate Ukrainian Charities 🇺🇦✊

Last edited by LoRd_MuldeR; 2nd January 2020 at 00:36.
LoRd_MuldeR is offline   Reply With Quote