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
- The pointer cannot be re-assigned to a different
GUID
- 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).
精彩评论