C++ reinterpret_cast
reinterpret_cast
reinterpret_cast用於將某種類型的指標強行轉換為另一種類型的指標。因為它不會做類型檢查,所以與static_cast比起來,它是較為危險的。
在TensorRT/blob/master/samples/common/common.h的函數readPGMFile中:
inline void readPGMFile(const std::string& fileName, uint8_t* buffer, int inH, int inW)
{
//...
infile.read(reinterpret_cast<char*>(buffer), inH * inW);
}
將uint8_t*型別的指標強行轉為char*型別的指標。
其中的uint8_t是一個在C99標準中,使用typedef定義於stdint.h的型別:
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
typedef unsigned long long uint64_t;
我們可以看出,uint8_t其實就是unsigned char,所以上面的reinterpret_cast其實是在char*及unsigned char*間做轉換。
參考連結
来源:CSDN
作者:keineahnung2345
链接:https://blog.csdn.net/keineahnung2345/article/details/104116238