Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QByteArray class input to function - pass by reference or pass by value???
Qt 6.11 is out! See what's new in the release blog

QByteArray class input to function - pass by reference or pass by value???

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 7.8k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    When passing a QByteArray variable to a function, is the variable pass by reference or pass by value?

    I have the following code in my program:

    @
    // global variable
    QByteArray RX_Buffer;

    // In one of my function, I am passing RX_Buffer to a function
    bool ParseMessage (QByteArray data, QByteArray rx_data)
    {
    // state machine processing to populate values to rx_data
    for(int i = 0; i < 10; i++)
    rx_data[i] = (quint8)i;
    }
    bool cls_ElectricityMeter::ReadDemo()
    {
    ParseMessage(TX_Buffer, RX_Buffer);
    return false;
    }
    @

    At the return point of ReadDemo function, RX_Buffer.data() does not contain the populated values from ParseMessage. Am I doing something wrong?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      If you want to change your method parameter you should pass it by reference (i.e. with &). In your case implicitly shared copy created at method call and after first assign sharing breaks.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        sorry but I do not quite understand . Can you provide a code snippet?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          nevermind... i got it!!! thanks a bundle. I assumed classes were pass by reference by default for some reason.

          1 Reply Last reply
          0

          • Login

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