If an operator is overloaded for a C++ class how c

2019-01-19 05:58发布

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

3条回答
地球回转人心会变
2楼-- · 2019-01-19 06:15

I define this utility function:

template<typename T>
T *GetRealAddr(T &t)
    { return reinterpret_cast<T*>(&reinterpret_cast<unsigned char &>(t)); }
查看更多
聊天终结者
3楼-- · 2019-01-19 06:15

&variable.GetInterfacePtr();

查看更多
Bombasti
4楼-- · 2019-01-19 06:24

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)

查看更多
登录 后发表回答