获取具有重载运算符的对象的地址

Get the address of an object that has overloaded operator&

本文关键字:对象 地址 运算符 重载 获取      更新时间:2023-10-16

CComPtroperator&过载,这使得我的一般编写的代码崩溃。

我想知道是否有任何方法可以强制变量返回其对象的地址?

解决方案很可怕:

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

如果可以使用boost,请考虑使用:addressof,如果不能,请查看实现。

您可以将CComPtr强制转换为CComPtrBase,根据我从MSDN页面收集到的信息,它没有重载运算符。它可能看起来像这样:

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

我不知道这是否有效(我不能尝试),但看起来可能,如果有效,它会比reinterpret_cast解决方案好一点。