Help me Understand const reference and direct and queued connections



  • what is meaning of this line in detail:
    For direct connections, it is recommended that the values are passed by const reference to avoid needless copies. For queued connections Qt will make a copy no matter how you pass the arguments.
    Please help me with some example if possible as it will be beneficial for others.


  • Moderators

    @guru007 When you pass something by reference it will not be copied. For instance, if you have a QString and you pass it by reference it will not copy the data.

    I.e.

    QString hi = "hello world";
    
    // ...
    
    void myFunction(QString &s)
    {
       // s is not a copy of "hi" but points to the same data.. if you change
       // s then you will change hi as well.
    }
    

    The benefit to this is saving on memory and the cpu cycles it takes to allocate and copy the class.

    When they say const reference that is typically used when you want a reference but you do not want it modified, same example above but with constness:

    QString hi = "hello world";
    
    void myFunction(const QString &s)
    {
       // s still points to the same data as hi but it is not modifiable since it is const
    }
    

    Again the benefit of passing by reference (const or not) is to prevent the copy of the object being passed. This saves memory and cpu time. And it can be quite significant depending on what is the variable being passed, and how many times it is passed around.

    One more quick example.. If you have a long string, say some content you loaded from a file that is 1kb in size. It is stored in a qbytearray. You have a function that takes a QByteArray and it isn't a reference (or a pointer) then it will copy the data. You now have 2kb allocated for 2 copies of that same data. Not to mention the cpu time to make the actual copy.

    Then say that function calls into another function and passes QByteArray again.. you are now up to 3kb. So you can see this problem can be compounded the more you pass it around.

    In my code I almost always pass things by reference or pointer (const or not) to save on these copies. In some cases I want to modify a copy of the data. I still tend to pass by ref or data but then make a specific copy i.e.:

    void processFile(const QString &filename)
    {
       // here I have a reference to a file name, but let's say I want to add a new extension.. I'll make my own copy
       auto newFilename = QString("%1.myext").arg(filename);
    
    }
    

    Hope that helps you understand. After I typed it out, it still seemed a bit confusing but I'm not the best c++ teacher. :) There are way better ones out there so just google c++ references and it should explain the benefits to using them.



  • thanks you very much @ambershark
    Now, Things are more clear to me and I will use them in daily coding practice.



  • @guru007 to add to @ambershark

    queued connections are the default way to pass Signals/objects/values from one thread to an other.

    To prevent simultaneous read/write access of the same memory, the arguments of queued connections are always copies.

    Also If you have a QueuedConnection inside the same thread. The connected SLot is only executed when the eventloop returns the next time. Meanwhile, your argument could have been changed already or, if its a local variable its already destroyed because the function finished
    -> Therefore a copy is created as soon as you emit the Signal.


Log in to reply
 

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