C++11 QString pass by reference?
-
Hi all,
I am starting a Qt5 project where I want to use C++11 exclusively.
Which is faster for QStrings now, pass by value or pass by reference?Cheers,
Martin -
QString is implicitly shared, so it's quite fast either way. You can benchmark if you prefer, I guess passing by reference would be a little bit faster, but probably not that much.
-
Here's an article for more details: http://qt-project.org/doc/qt-5.0/qtcore/implicit-sharing.html
As sierdzio said, there's probably no noticeable difference between passing by value or passing by reference, because QString is very highly optimized already
-
I always pass by reference because of two reasons
1 : pass by reference don't need to take the price of reference count
2 : it is more natural since the stl containers do not ask to use reference count technique,
many c++ programmers may feel weird when they see you pass by value rather than pass by reference.