CPP {类型强转,reinterpret_cast,static_cast}
@LOC_1
@LOC: 2
static_cast
性质
ST * s = new ST
此时static_cast<ST*>(s) != nullptr
, 而如果你s = nullptr
后 会发现static_cast<ST*>(s) == nullptr
这说明 他看的 不是ST *
这个静态类型 而是看他的值;
@DELI;
static_cast<T>(a)
和(T)a
方式, 并不是等价的; (比如@LINK: @LOC_0
);
reinterpret_cast
定义
#用途1#
指针的强转, 从void*
转换成T*
;
.
将T*
转换为void*
这是可以隐式强转的, 但反过来 给你一个void*
(但前提是你知道 他本质上是T*
), 他不可以直接强转到T*
, 那么此时 这就可以T* = reinterpret_cast<T*>(void*)
;
#用途2#
将指针的地址 转换为 整数类型;