如何将自定义类指针转换为无符号int
How to convert custom class pointer to unsigned int
我需要键入指向无符号int值的强制转换自定义类指针。这是我累的东西。
Class A{
};
int main()
{
A* a = foo();
unsigned int handler = reinterpret_cast<unsigned int>(a);
return 0;
}
它发出警告warning C4302: 'reinterpret_cast': truncation from 'A *' to 'unsigned int'
在C++方式中克服这一问题的正确方法是什么?
该错误可能意味着您正在编译到64位目标,在这种情况下,指针是64位的,重新解释到int意味着截断(因为int是atmost 32位)。
不确定要对处理程序做什么,但更安全的方法是,如果c++11可用,则强制转换为uintptr_t。
http://en.cppreference.com/w/cpp/types/integer
如果包含<cstdint>
,它应该类似于
uintptr_t handler = reinterpret_cast<uintptr_t>(a);
相关文章:
- 将无符号char*转换为std::istream*C++
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- 获取隐式转换溢出从无符号到已签名的警告
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 是什么导致了这种使用三进制而不是短整型的有符号int到无符号int转换
- 无法在 Arduino 中uint8_t数组转换为无符号长整型数组
- C ++中无符号位长度类型之间的隐式转换,即uint8_t,uint16_t
- 将浮点数转换为无符号字符数组并打印出来
- 如何在 C/C++ 中将无符号字符*转换为无符号字符数组?
- 将无符号转换为复杂<int>原因符号转换警告
- 如何解决隐式转换丢失整数精度:'size_t'(又名"无符号长")到'int'警告?
- 如何将带有十六进制值的 std::string 转换为 std::vector<无符号字符>
- 将无符号字符 C++ 转换为 C#
- C++:使用没有位移位的指针将无符号字符转换为无符号 int
- 为什么我可以隐式地将字符*转换为常量字符*,但不能将无符号字符*
- 整数类型应该显式转换(例如"int"到"无符号")还是只会增加混乱?
- 将字符串转换为无符号字符数组/字节数组
- 转换“;无符号字符*”;至“;char*”;(和字符串)