Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Differences between Qt cast and normal cast (and some questions about it)

Differences between Qt cast and normal cast (and some questions about it)

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 304 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MihanM Offline
    MihanM Offline
    Mihan
    wrote on last edited by Mihan
    #1

    Hi
    I want to know the differences between the cast like static_cast and (type)param

    e.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 wiki

    Regards
    Mihan

    aha_1980A 1 Reply Last reply
    0
    • MihanM Mihan

      Hi
      I want to know the differences between the cast like static_cast and (type)param

      e.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 wiki

      Regards
      Mihan

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi @Mihan,

      the absolutely clean way is indeed to have a uint8_t buffer[] and to memcpy the QByteArray contents to it.

      But if your API takes a const uint8_t *data pointer, I'd indeed go with reinterpret_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

      Qt has to stay free or it will die.

      1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Out of curiosity, what Qt cast do you have in mind ? Is see only C/C++ cast in your post.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved