gpt4 book ai didi

c++ - 为什么 *&x 与 x 不同?

转载 作者:可可西里 更新时间:2023-11-01 17:10:47 25 4
gpt4 key购买 nike

简短版:

以下代码无法编译:

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;

最佳答案

因为一元运算符 &* 可以重载,我猜 CComBSTR 会重载。

* 更新:*

对于那些想知道如何获取其类型已重载operator& 的变量的地址的人,可以使用 TR1 的 std::addressof,以及它的一个针对 C 的 Boost 实现++03 兼容性。

关于c++ - 为什么 *&x 与 x 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7699588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com