2009-11-27 69 views
10

如果是这样,为什么一些Win32头文件使用它?“const LPVOID”是否等同于“void * const”?

例如:

BOOL APIENTRY VerQueryValueA(const LPVOID pBlock, 
    LPSTR lpSubBlock, 
    LPVOID * lplpBuffer, 
    PUINT puLen 
    ); 

多一点的阐述:如果API从不使用引用(或任何其他C++ - 只建),但只有指针和价值观,什么是具有const LPVOIDLPCVOID点。

我是否应该把每个地方看作const LPVOID作为一些地方的真正意义是LPCVOID? (因此可以安全地添加演员表)

进一步说明:看起来const LPVOID pBlock在这种情况下确实是一个错误。 Windows 2008 SDK将其替换为LPCVOID中的VerQueryValue签名。葡萄酒在很久以前就这样做了。

+1

我听说他们'定义'这些东西,所以它宁可是'const void *'。如果它们是typedef,那么确实会是'void * const'。 – 2009-11-27 12:26:27

+0

@litb:不幸的是这些都是typedefs – EFraim 2009-11-27 12:28:22

+0

@EFraim啊,我明白了。被诅咒的大写:) – 2009-11-27 12:35:24

回答

12

一个typedef的名称是指一个类型,而不是标记序列(如做一个宏)。在你的情况下,LPVOID表示也由标记序列void *表示的类型。所以图看起来像

// [...] is the type entity, which we cannot express directly. 
LPVOID => [void *] 

语义如果指定类型const LPVOID,你会得到如下图(绕符的括号表示“由符表示的类型”):

// equivalent (think of "const [int]" and "[int] const"): 
const LPVOID <=> LPVOID const => const [void *] <=> [void *] const 
           => ["const qualified void-pointer"] 

这是而不是与令牌序列const void *相同 - 因为这个不会表示一个const限定的指针类型,而是一个指向const限定类型的指针(指向的对象将是const)。

句法参数声明具有以下(简化的)形式:

declaration-specifiers declarator 

声明-说明符中的const void *p情况下是const void - 这样的基型*p是一个const合格void,但指针本身不合格。但是,在const LPVOID p的情况下,声明说明符会指定一个具有const限定的LPVOID - 这意味着指针类型本身是合格的,从而使参数声明与void *const p相同。

+1

等一下,现在我很困惑。 const void *和void * const不一样。 – EFraim 2009-11-27 12:54:28

+1

@EFraim,'void *'旁边的框表示它是由'LPVOID'表示的类型 - 并不意味着文本出现在声明中。 – 2009-11-27 12:57:30

+0

啊,好的,这张图让我感到困惑。 – EFraim 2009-11-27 13:02:32

0

LPVOID是远泛指针,它已经很长时间与普通通用指针相同了(它在旧的16位平台上有所不同)。

+1

这是好的,但与问题的主题无关。 – EFraim 2009-11-27 13:16:38

0
void* const x = 0; 
x = 0; // this line will not compile - u cannot change x, only what it points to 
x->NonConstMethod(); // will compile 
const void* y = 0; 
y = 0; // this line will compile - u can change y, but not what it points to 
y->NonConstMethod(); // will not compile 
const void* const z = 0; // u cannot change z or what it points to 
// btw, the type of the 'this' pointer is "ClassName* const this;" 
相关问题