为什么 *&x 与 x 不同?
Why is *&x not the same as x?
简写:
下面的代码不能编译:
CComBSTR temp;
CMenu().GetMenuString(0, temp, 0);
,但这是:
CComBSTR temp;
CMenu().GetMenuString(0, *&temp, 0);
为什么?
完整代码:
#include <atlbase.h>
extern CComModule _Module;
#include <atlapp.h>
#include <atlwin.h>
#include <atlctrls.h>
int main() {
CComBSTR temp;
CMenu().GetMenuString(0, *&temp, 0);
}
GetMenuString
签名(from atluser.h
, from WTL):
BOOL GetMenuString(UINT nIDItem, BSTR& bstrText, UINT nFlags) const;
因为一元运算符&
和*
可以被重载,我猜CComBSTR
可以。
* Update: *
对于那些想知道如何获得类型重载operator&
的变量的地址的人来说,有TR1的std::addressof
,以及为c++ 03兼容性提供的Boost实现。
CComBSTR
可能重载了operator *
或operator &
,以返回与GetMenuString()
接收的参数类型匹配的类型
因此,虽然*&x
与内置数据类型的x
相同,但对于用户定义的类型可能不相同。
operator&
在CComBSTR
上被重载并返回BSTR*
,因此解引用它会给您所需的类型,即BSTR
。
如果没有编译,那么您应该有一个有意义的错误消息?
函数定义如下,需要BSTR&
:
BOOL GetMenuString(UINT nIDItem, BSTR& bstrText, UINT nFlags) const
CComBSTR
类本身不会强制转换为BSTR&
,但是通过& operator
和* operator
,它可以。
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 为什么mpfr_printf与十六进制浮点(%a转换说明符)的printf不同
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 为什么std::vector和std::valarray初始化构造函数不同
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 为什么 gcc 和 clang 为函数模板的实例化生成不同的符号名称?
- 为什么C++有不同的变量初始化方式?
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 为什么 GCC 在使用类型别名时处理 const reinterpret_cast不同?
- 为什么:不同符号的整数比较只是偶尔发生?
- 为什么兰德每次都给我几乎相同(但略有不同)的数字
- R值和L值的输出不同.为什么?
- Visual Studio 2012应用程序窗口创建/调整大小与VS2008窗口创建不同?为什么?
- Boost::thread::interrupt() 对于不同的中断点,其行为不同.为什么
- 进程管理代码在Linux和Windows上的行为不同-为什么
- c++中double的精确位数在windows和Linux中不同.为什么?Linux显示超过20个非零精度数字
- 为什么-i++和-(i++)不同?为什么结果如下