以下代码无法编译:
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
签名(来自atluser.h
,来自WTL):
BOOL GetMenuString(UINT nIDItem, BSTR& bstrText, UINT nFlags) const;
答案 0 :(得分:11)
因为一元运算符&
和*
可以重载,我猜CComBSTR
就是这样。
*更新:*
对于那些想知道如何获取类型重载operator&
的变量的地址的人来说,有TR1的std::addressof
,以及它的Cost 03兼容性的Boost实现。
答案 1 :(得分:4)
CComBSTR
可能会重载operator *
或operator &
以返回与GetMenuString()
因此,虽然*&x
与内置数据类型的x
相同,但对于用户定义的类型可能不同。
答案 2 :(得分:4)
operator&
上的{p> CComBSTR
已超载并返回BSTR*
,因此取消引用它会为您提供所需的类型,即BSTR
。
答案 3 :(得分:0)
如果它没有编译,那么你应该有一个有意义的错误信息?
该功能定义如下,并希望BSTR&
:
BOOL GetMenuString(UINT nIDItem, BSTR& bstrText, UINT nFlags) const
CComBSTR
类不会转发给BSTR&
本身,而是通过& operator
后跟* operator
。