Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange Qt Code with strings

Tags:

c++

qt

qstring

I found my friend's Qt code and he uses the modulo operator on two QStrings like this:

QString result = oneString % twoString;

What does it mean?

like image 456
rubenvb Avatar asked Aug 19 '12 16:08

rubenvb


2 Answers

It's just another (more efficient) way to concatenate QStrings as described in the manual

QStringBuilder uses expression templates and reimplements the '%' operator so that when you use '%' for string concatenation instead of '+', multiple substring concatenations will be postponed until the final result is about to be assigned to a QString. At this point, the amount of memory required for the final result is known. The memory allocator is then called once to get the required space, and the substrings are copied into it one by one.

like image 149
cnicutar Avatar answered Nov 04 '22 12:11

cnicutar


It is Qt specific way of string construction. Take a look on this page.

QStringBuilder uses expression templates and reimplements the '%' operator so that when you use '%' for string concatenation instead of '+', multiple substring concatenations will be postponed until the final result is about to be assigned to a QString. At this point, the amount of memory required for the final result is known. The memory allocator is then called once to get the required space, and the substrings are copied into it one by one.

like image 45
besworland Avatar answered Nov 04 '22 14:11

besworland