views:

150

answers:

4

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

Thanks!

+3  A: 

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
JaredPar
+4  A: 

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

Preet Sangha
I suspect you're right. I bet Joe mixed the C declaration for REFGUID and the C++ definition for IsEqualGUID.
Drew Hoskins
Great feedback Preet. I copied the code straight from the book. Which appears to be wrong given the feedback I see in the answers. If the book illustrated the memcmp to appear as you state above then I wouldn't have been thrown by the syntax necessitating the question on SO. Thanks for the info!
Joe
+3  A: 

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.

Sherwood Hu
not helpful or topical.
Drew Hoskins
actually this perfectly addresses the original poster's question. up-voting.the memcmp Joe was asking about only makes sense with the C++ REFGUID definition.
Jewel S
+1  A: 
#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).

f0b0s