views:

185

answers:

3

_com_ptr_ has an overloaded operator&() with a side effect. If I have a variable:

_com_ptr_t<Interface> variable;

How could I retrieve its address (_com_ptr_t<Interface>* pointer) without calling the overloaded operator and triggering the side effect?

A: 

&variable.GetInterfacePtr();

Goz
This retrieves the address of the pointer stored in the smart pointer. However, I think question is about how to get the address of the smart pointer.
Martin Liversage
Ah ... my bad :(
Goz
+7  A: 

I've seen this case pop up in an ISO meeting as it broke some offsetof() macro implementations (LWG 273). The solution: &reinterpret_cast<unsigned char&>(variable)

MSalters
Could this ever bite me in some surprising case?
sharptooth
use boost::address_of. It is based on implementation provided in this post. I think it will a part of the upcoming standard.
ovanes
MSalters
+4  A: 

I define this utility function:

template<typename T>
T *GetRealAddr(T &t)
    { return reinterpret_cast<T*>(&reinterpret_cast<unsigned char &>(t)); }
Daniel Earwicker