a****f 发帖数: 944 | 1 A phone interview question. I said using reinterpret_cast, but he said no.
Does any one know the correct answer? I wasn't a CS major so I know little
about fundamental C++. |
C***y 发帖数: 2546 | 2 直接强制转换就行了
比如说加(int *)
如果在C++中的话,可以加static_cast
【在 a****f 的大作中提到】 : A phone interview question. I said using reinterpret_cast, but he said no. : Does any one know the correct answer? I wasn't a CS major so I know little : about fundamental C++.
|
a****f 发帖数: 944 | 3 Thanks.
Then was my answer (reinterpret_cast) wrong? if so what's wrong with it?
【在 C***y 的大作中提到】 : 直接强制转换就行了 : 比如说加(int *) : 如果在C++中的话,可以加static_cast
|
d*****d 发帖数: 46 | 4 I agree with you. in c++, we should use reinterpret_cast.
【在 a****f 的大作中提到】 : Thanks. : Then was my answer (reinterpret_cast) wrong? if so what's wrong with it?
|
l*****g 发帖数: 685 | 5 reinterpret_cast和直接用hard cast都应该可以啊
(至于得到的 pointer address是不是有效,那是另一回事)
如果反过来,把pointer convert 成 int, 那得考虑是32位还是64位的系统。如果是64
位的,那convert to int 会有loss
【在 a****f 的大作中提到】 : A phone interview question. I said using reinterpret_cast, but he said no. : Does any one know the correct answer? I wasn't a CS major so I know little : about fundamental C++.
|
l*****g 发帖数: 685 | 6 static_cast不行的
static_cast必须在匹配的类型之间,不能把bits随意cast成任何类型
【在 C***y 的大作中提到】 : 直接强制转换就行了 : 比如说加(int *) : 如果在C++中的话,可以加static_cast
|
C***y 发帖数: 2546 | 7 试了一下
的确不行
什么样才叫匹配的类型?
谢谢
【在 l*****g 的大作中提到】 : static_cast不行的 : static_cast必须在匹配的类型之间,不能把bits随意cast成任何类型
|
r********g 发帖数: 1351 | 8 就是float->int, char->int这类的吧,编译器可以自动转换的类型,加上static cast
是为了防止
bug和理解方便。
【在 C***y 的大作中提到】 : 试了一下 : 的确不行 : 什么样才叫匹配的类型? : 谢谢
|
C***y 发帖数: 2546 | 9 thanks
cast
【在 r********g 的大作中提到】 : 就是float->int, char->int这类的吧,编译器可以自动转换的类型,加上static cast : 是为了防止 : bug和理解方便。
|
F**********r 发帖数: 237 | 10 static_cast can be used when the compiler can do the conversion
automatically(with a warning msg). imho reinterpret_cast is the only way. no
? |