这些老式的演员表怎么办?

时间:2018-09-26 09:01:02

标签: c++ casting

我得到了一些我想摆脱的“使用老式样式转换”警告,但是我对此并不了解。

编辑: HKEY_CURRENT_USER确实是在WinAPI中定义的,因此我将不理会它。

(LPBYTE)&resultLPBYTE(&result)reinterpret_cast<LPBYTE>(&result)可以工作,但是我不知道两者是否相等。 那么我应该使用这三个中的哪个?

(const BYTE*)&valuereinterpret_cast<const BYTE*>(&value)有用,但又是一样。 那我该用哪两个呢?

更多背景信息:

HKEY hKey;
std::string sResult = "";
if(regOpenKey(KEY_READ, &hKey))
{
    DWORD size=1024, type = REG_SZ;
    wchar_t result[MAX_PATH];
    if(RegQueryValueEx(hKey, key, nullptr, &type, (LPBYTE)&result, &size) == ERROR_SUCCESS)
        sResult = str_narrow(result);
}

RegCloseKey(hKey);

和:

HKEY hKey;
if(regOpenKey(KEY_ALL_ACCESS, &hKey))
{
    DWORD value = 1;
    RegSetValueEx(hKey, key, 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
}

RegCloseKey(hKey);

感谢您的帮助:)

1 个答案:

答案 0 :(得分:3)

HKEY_CURRENT_USER是在WinAPI中定义的,因此请不要理会它。它可能会更改而不会发出警告(尽管不太可能,但是可能)。

  

(LPBYTE)&result: LPBYTE(&result)有效,但是我不知道它是否等效。

是的,相同。

  

(const BYTE*)&value: reinterpret_cast<const BYTE*>(&value)有效,但还是一样。

还是一样。

相关问题