Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If an operator is overloaded for a C++ class how could I use a default operator instead?

_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?

like image 908
sharptooth Avatar asked Jul 17 '09 11:07

sharptooth


2 Answers

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)

like image 126
MSalters Avatar answered Sep 19 '22 18:09

MSalters


I define this utility function:

template<typename T>
T *GetRealAddr(T &t)
    { return reinterpret_cast<T*>(&reinterpret_cast<unsigned char &>(t)); }
like image 43
Daniel Earwicker Avatar answered Sep 21 '22 18:09

Daniel Earwicker