了解指针引用的工作原理

发布于 2024-08-10 03:27:33 字数 654 浏览 2 评论 0原文

我目前正在阅读“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别再吹冷风 2024-08-17 03:27:33

REFGUID 是常量 guid 的常量 ptr(即两者都不能改变)。

代码不应该是吗?

 BOOL IsEqualGUID(REFGUID rguid1, REFGUID rguid2)  
 {        
   return !memcmp(rguid1, rguid2, sizeof(GUID));      
 }

正如 memcmp 所采取的那样:

int memcmp(const void *s1, const void *s2, size_t n);

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?

 BOOL IsEqualGUID(REFGUID rguid1, REFGUID rguid2)  
 {        
   return !memcmp(rguid1, rguid2, sizeof(GUID));      
 }

as memcmp takes:

int memcmp(const void *s1, const void *s2, size_t n);

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

静谧幽蓝 2024-08-17 03:27:33

REFGUID 在 C++ 和 C 上下文中的定义不同。如果你看一下它的定义,那就是:

#ifdef __cplusplus
#define REFGUID const GUID &
#else
#define REFGUID const GUID * 
#endif

IsEqualGUID()函数也有不同的实现。

我不喜欢这个主意。我猜这个人发明这个只是为了让它“C++正确”,因为C++发明者相信引用比指针更好。

REFGUID is defined differently in C++ and C context. If you look at its definition, it is:

#ifdef __cplusplus
#define REFGUID const GUID &
#else
#define REFGUID const GUID * 
#endif

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.

遇到 2024-08-17 03:27:33

定义 REFGUID 是指向 GUID 的指针,满足以下条件

  1. 指针不能重新分配给不同的 GUID
  2. 通过指针访问时 GUID 的内容被认为是常量

The define REFGUID is a pointer to a GUID for which the following is true

  1. The pointer cannot be re-assigned to a different GUID
  2. The contents of the GUID when accessed through the pointer are considered const
梦巷 2024-08-17 03:27:33
#define REFGUID const GUID * const

等于(不是 C++ 代码,抽象!)

const GUID * const  ==  REFGUID 

并且它等于,

GUID const  * const  ==  REFGUID 

因此它是指向 const GUID 对象(无法更改值)的 const 指针(无法更改指针)。

#define REFGUID const GUID * const

is equal to (not C++ code, abstract!)

const GUID * const  ==  REFGUID 

and it is equal to

GUID const  * const  ==  REFGUID 

so it is const pointer (can't change poiter) to a const GUID object (can't change the value).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文