C++ reinterpret_cast

≡放荡痞女 提交于 2020-01-31 04:32:29

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*間做轉換。

參考連結

reinterpret_cast in C++ | Type Casting operators

When to use reinterpret_cast?

uint8_t vs unsigned char

Dinkum Documentation > Dinkum C99 Library - <stdint.h>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!