gpt4 book ai didi

c++ - C 风格的指针查询到 C++ 类型转换

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:09 24 4
gpt4 key购买 nike

typedef uint8_t RANGE;

int main()
{
const uint16_t FREQ_RATED_MIN_VALUE = 54;

RANGE* min;

min = (uint8_t*)&FREQ_RATED_MIN_VALUE; // C style tyecast

cout << min << endl; // prints value

min = reinterpret_cast<uint8_t*>(&FREQ_RATED_MIN_VALUE); //ERROR to be removed in this statement to be written in C++ style typecast

return 0;
}

需要帮助在注释语句中进行 C++ 样式类型转换..

最佳答案

你的转换丢弃了 const 资格。使用 C++ 风格的转换来摆脱 constness 需要 const_cast。试试这个:

min = reinterpret_cast<uint8_t*>(const_cast<uint16_t*>(&FREQ_RATED_MIN_VALUE));

关于c++ - C 风格的指针查询到 C++ 类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24255684/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com