Get the address of an object that has overloaded operator&

雨燕双飞 提交于 2019-12-24 10:58:47

问题


operator& for CComPtr is overloaded, which makes my generically written code crash.

I am wondering if there is any way to force a variable into returning the address of it's object?


回答1:


The solution is hideous:

reinterpret_cast<CComPtr*>(&reinterpret_cast<char&>(ptr))



回答2:


If you can use boost, consider using: addressof, if you cannot, take a look at the implementation.




回答3:


You could cast the CComPtr to a CComPtrBase, which, from what I can gather from the MSDN page, has no overloaded operator. It might look like this:

CComPtr* addr=dynamic_cast<CComPtr*>(&((CComPtrBase)theCComPtr));

I don't know if this works (I can't try it), but it seems like it might, and if it does, it's a little bit better then the reinterpret_cast solution.



来源:https://stackoverflow.com/questions/9536070/get-the-address-of-an-object-that-has-overloaded-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!