Differences between Qt cast and normal cast (and some questions about it)
-
Hi
I want to know the differences between the cast likestatic_castand(type)parame.g. some protocols' API will need `uint8_t*` param: `bool writedata(uint8_t* data);` but the data type from qt `QByteArray` is `char*`some blogs say that it's unsafe to use
reinterpret_cast. Could you tell me how to use these casts?I can't find the information about this in wikiRegards
Mihan -
Hi
I want to know the differences between the cast likestatic_castand(type)parame.g. some protocols' API will need `uint8_t*` param: `bool writedata(uint8_t* data);` but the data type from qt `QByteArray` is `char*`some blogs say that it's unsafe to use
reinterpret_cast. Could you tell me how to use these casts?I can't find the information about this in wikiRegards
MihanHi @Mihan,
the absolutely clean way is indeed to have a
uint8_t buffer[]and tomemcpytheQByteArraycontents to it.But if your API takes a
const uint8_t *datapointer, I'd indeed go withreinterpret_cast:QByteArray ba = "..."; const uint8_t *data = reinterpret_cast<const uint8_t *>(ba.constData());Note that functions non-const parameters (
uint8_t *data) might modify the data, which can lead to unexpected side effects of any kind when they operate on the byte array data.Regards
Regards
-
Hi,
Out of curiosity, what Qt cast do you have in mind ? Is see only C/C++ cast in your post.