了解指针引用的工作原理
我目前正在阅读“COM 和 ATL 3.0 的开发人员研讨会”。第 3 章介绍 GUID、引用和比较。指针很痛苦。我可以使用一些帮助来破译 REFGUID #define
(见下文)以及 IsEqualGUID
中的 memcmp
如何针对指针工作。
给定:
typedef struct_GUID{ unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8]; } GUID;
我如何解释这个#define
?:
#define REFGUID const GUID * const
&rguid1
如何寻址传入变量?
BOOL IsEqualGUID(REFGUID rguid1, REFGUID rguid2)
{
return !memcmp(&rguid1, &rguid2, sizeof(GUID));
}
I am currently reading "Developer's Workshop to COM and ATL 3.0". Chapter 3 introduces GUIDs, referencing and comparisons. Pointers are painful. I could use some help in deciphering the REFGUID #define
(see below) and how memcmp
in IsEqualGUID
works against the pointers.
Given:
typedef struct_GUID{ unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8]; } GUID;
How do I interpret this #define
?:
#define REFGUID const GUID * const
How is the &rguid1
addressing the incoming variable?
BOOL IsEqualGUID(REFGUID rguid1, REFGUID rguid2)
{
return !memcmp(&rguid1, &rguid2, sizeof(GUID));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
REFGUID 是常量 guid 的常量 ptr(即两者都不能改变)。
代码不应该是吗?
正如 memcmp 所采取的那样:
memcmp 应该传递指针(rguidx)而不是指针的地址。
如果看起来代码最初是用 REGUID 定义为 const GUID 或 const GUID 引用(C++)编写的
The REFGUID is constant ptr to a constant guid (ie neither can change).
Shoud the code not be?
as memcmp takes:
The memcmp should be passed the pointers (rguidx) not the address of the pointer.
if looks like the code was originally written with REGUID defined as a const GUID or const GUID reference (C++) perhaps
REFGUID 在 C++ 和 C 上下文中的定义不同。如果你看一下它的定义,那就是:
IsEqualGUID()函数也有不同的实现。
我不喜欢这个主意。我猜这个人发明这个只是为了让它“C++正确”,因为C++发明者相信引用比指针更好。
REFGUID is defined differently in C++ and C context. If you look at its definition, it is:
IsEqualGUID() function also has different implementations.
I do not like this idea. I guess that the person invented this just to make it "C++ right" because the C++ inventor believes that reference is better than pointer.
定义 REFGUID 是指向
GUID
的指针,满足以下条件GUID
The define REFGUID is a pointer to a
GUID
for which the following is trueGUID
等于(不是 C++ 代码,抽象!)
并且它等于,
因此它是指向 const GUID 对象(无法更改值)的 const 指针(无法更改指针)。
is equal to (not C++ code, abstract!)
and it is equal to
so it is const pointer (can't change poiter) to a const GUID object (can't change the value).