代码之家  ›  专栏  ›  技术社区  ›  EFraim

“const lpvoid”是否等同于“void*const”?

  •  10
  • EFraim  · 技术社区  · 15 年前

    如果是这样,为什么有些win32头文件会使用它?

    例如:

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

    更详细的说明:如果API从不使用引用(或者任何其他C++的构造),而只使用指针和值,那么它的意义是什么呢? const LPVOID VS LPCVOID .

    我应该把我看到的每一个地方都治好吗 常量词 作为真正意义所在的地方 LPCVALL ?(因此,添加石膏是安全的)

    进一步澄清:似乎 const LPVOID pBlock 在这件事上确实是个错误。Windows 2008 SDK将其替换为 LPCVALL 在里面 VerQueryValue 签名。酒在很久以前就这么做了。

    4 回复  |  直到 14 年前
        1
  •  13
  •   Johannes Schaub - litb    15 年前

    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 * -因为这个不会表示一个常量限定的指针类型,而是一个指向常量限定类型的指针(指向的东西将是常量)。

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

    declaration-specifiers declarator
    

    声明说明符 const void *p const void -所以基本类型 *p 常量是否合格 void 但指针本身不合格。万一 const LPVOID p 但是声明说明符指定了一个常量限定符 L空隙 -这意味着指针类型本身是限定的,使参数声明与 void *const p .

        3
  •  0
  •   denisenkom    15 年前

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

        4
  •  0
  •   Benjamin Cintix    14 年前
    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;"