Question

I am always in a doubt on when I should reference and when I should use a variable pass. For example, one of the API is called by the JOBJECTs -

QLIST retrive_cinemas(QString& options)
{
   return (staticobject::getcinemas(options));
}

or

QLIST retrive_cinemas(QString options)
{
   return (staticobject::getcinemas(options));
}
Was it helpful?

Solution

It seems to me that your problem can be reduced to something like this:

You have a function/method f(), and a class X, and you want to know if/when X should be passed to f() by reference or not.

You can identify three options:

void f(X v)            // #1 - pass by value
void f(const X& cr)    // #2 - pass by const reference (&)
void f(X& r)           // #3 - pass by reference (&)
  1. If X is cheap to copy (e.g. it's an int, a double, etc.), and you do not want to modify it, then pass by value (#1).

  2. If X is not cheap to copy (e.g. it's a vector, a string, etc.), and yo do not want to modify it, then pass by const reference (#2).

  3. If you want to modify the argument of type X inside f(), then pass by reference.


In the particular code you posted, since QString is a full-fledged class which is not cheap to copy as e.g. an int or a double (even if it uses COW techniques and "implicit sharing", I believe that copying still implies a call to something like Win32 InterlockedIncrement() for increasing the ref count in a thread-safe atomic way), I'd pass it by const reference (i.e. const QString &, #2) if you do not want to modify it inside the function.
Just pass by reference (QString&, #3) if you want to modify it inside the function's body.

OTHER TIPS

In Qt the answer depends on whether the object you would like to pass uses implicit sharing or not:

Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying. Implicitly shared classes are both safe and efficient when passed as arguments, because only a pointer to the data is passed around, and the data is copied only if and when a function writes to it, i.e., copy-on-write.

You can but you need not pass objects using implicit sharing by reference. They are designed to be passed by value efficiently!

Here you can find the complete explanation and the list of classes using implicit sharing. QString uses implicit sharing.

In Qt strings are implicitly shared and automatically copied on edit, so they are safe to pass even by value. It is still good practice to pass by reference though (in case it is not a QString), and it is even a tiny bit more efficient, since less data is copied, one memory address vs memory address, size and reference counting.

Generally speaking, it is a good idea to pass by reference when you want to modify the actual object inside the function (note that if you pass implicitly shared QString by value and modify it inside the function, this will not modify the original string but copy it and the changes will be lost after the function returns(unless you return the new string of course)), using references is a little more convenient than using pointers, and a little safer too. Also, if the object is larger than a primitive, or the object cannot/should not be copied, you can pass as reference. If you don't want to modify the source object, just make the reference const.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top