October 22, 2010

phamtv phamtv
Lab Rat
85 posts

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:

  1. // global variable
  2. QByteArray RX_Buffer;
  3.  
  4. // In one of my function, I am passing RX_Buffer to a function
  5. bool ParseMessage (QByteArray data, QByteArray rx_data)
  6. {
  7.      // state machine processing to populate values to rx_data
  8.      for(int i = 0; i < 10; i++)
  9.           rx_data[i] = (quint8)i;
  10. }
  11. bool cls_ElectricityMeter::ReadDemo()
  12. {
  13.     ParseMessage(TX_Buffer, RX_Buffer);
  14.     return false;
  15. }

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

3 replies

October 23, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1607 posts

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.

October 23, 2010

phamtv phamtv
Lab Rat
85 posts

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

October 23, 2010

phamtv phamtv
Lab Rat
85 posts

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

 
  ‹‹ QByteArray comparator      QScriptable inheritance. ››

You must log in to post a reply. Not a member yet? Register here!