开发者

Understanding how the pointer referencing works

开发者 https://www.devze.com 2022-12-10 11:21 出处:网络
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 #defin

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));  
    }  


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


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.


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


#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).

0

精彩评论

暂无评论...
验证码 换一张
取 消