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



  • 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?



  • 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.



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.