It seems to me that your problem can be reduced to something like this:
You have a function/method
f()
, and a classX
, and you want to know if/whenX
should be passed tof()
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 (&)
If
X
is cheap to copy (e.g. it's anint
, adouble
, etc.), and you do not want to modify it, then pass by value (#1).If
X
is not cheap to copy (e.g. it's avector
, astring
, etc.), and yo do not want to modify it, then pass by const reference (#2).If you want to modify the argument of type
X
insidef()
, 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.