I have got a function which I need to pass a value to for read-only purposes only.
For example:
unsigned short strlen(String str)
{
short i = 0;
while(str[i] != '\0')
i++;
return i;
}
As you can see, I do not want to change the original value I work with, I only need to read its length. Would it - in terms of performance - be better to replace the parameter String str
, which is making a copy of the original variable, by a reference like String &str
?
Would it - in terms of performance - be better to replace the parameter String str, which is making a copy of the original variable, by a reference like String &str?
Yes, it would. Instead of creating a copy, which you do not seem to need here, a reference would be bound. Since you are not modifying the object, a reference to const
would be better:
unsigned short strlen(String const& str)
// ^^^^^^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.