2009-11-27 47 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;" 
相關問題