获取具有重载运算符的对象的地址
Get the address of an object that has overloaded operator&
CComPtr
的operator&
过载,这使得我的一般编写的代码崩溃。
我想知道是否有任何方法可以强制变量返回其对象的地址?
解决方案很可怕:
reinterpret_cast<CComPtr*>(&reinterpret_cast<char&>(ptr))
如果可以使用boost,请考虑使用:addressof,如果不能,请查看实现。
您可以将CComPtr
强制转换为CComPtrBase
,根据我从MSDN页面收集到的信息,它没有重载运算符。它可能看起来像这样:
CComPtr* addr=dynamic_cast<CComPtr*>(&((CComPtrBase)theCComPtr));
我不知道这是否有效(我不能尝试),但看起来可能,如果有效,它会比reinterpret_cast
解决方案好一点。
相关文章:
- 空基优化子对象的地址
- 对象地址是否保证是其类型对齐的倍数
- 更改保留指向其字段的原始指针的对象地址
- 指向未由对象地址初始化的对象的指针如何将值分配给类的数据成员
- 为什么存储对象地址在缓冲区中会导致内存泄漏并删除它们
- 尝试将派生类对象地址分配给基类指针的向量
- 如果类型相同,对象地址必须不同,但如果类型不同,地址可以相同
- 使用对象地址以及方法地址和签名调用C 成员函数
- 将派生的对象地址分配给基础指针和调用击路仪的机制
- 为什么写入临时字符串流对象只打印对象地址
- 依赖静态对象地址安全吗
- 获取引用指向的对象地址
- 传递对象地址时"Address expression must be an lvalue or a function designator"
- 在C++中存储对象地址的数据结构
- 从函数初始化对象地址
- 使用reintepret_cast解释对象地址
- 为什么基类指针总是指向基类,即使它持有派生类对象地址
- 是否可以将堆栈对象地址赋给placement new
- <<运算符重载不起作用;仍在打印对象地址
- 关于返回对象地址的困惑