Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Qt classes by const reference

It's known that Qt classes use copy-on-wite when passing by value. So copy isn't done until its needed. I have seen many times passing Qt classes by const reference when only needed read-only access to object. Why do people pass const QString& instead of simple QString if in both cases no copy is done?

like image 637
Ashot Avatar asked Jun 12 '13 17:06

Ashot


1 Answers

This is because magic comes with a price. QString doesn't copy entire string, but it calculates references. Many copyings of QString can slow down the program. If const QString& is sufficient for your needs, why not use it? It is still faster.

like image 51
Pavel Strakhov Avatar answered Oct 21 '22 11:10

Pavel Strakhov