Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Use const std::string& in FontEngine functions #289
From an anon on OpenGameArt:
Maybe you should check the file FontEngine .*:
To const ref:
This comment has been minimized.
This comment has been minimized.Show comment Hide comment
Yeah, that's a pretty big one really. Passing a std::string object by value isn't as bad as it could be because most std::string implementations are implemented using reference counting, so the whole string isn't copied in memory, but it's much more expensive than passing by pointer or reference. It's even one of Scott Meyer's Effective C++ items "Prefer pass-by-reference-to-const to pass-by-value"
But also, in C++ it's almost always better to pass by reference as opposed to pointer. The only real exceptions I can think of are when the result may be "no object" (i.e., null pointer), when you need to re-assign the value or are using pointer arithmetic. Otherwise, the advantages of using references are:
And finally, when a member function will not (or is not supposed to) modify the object, it's better to declare the function as const, telling the compiler that it has no side-effects. This promotes the usage of const values, which have more benefits than I can really think of right now. :) So I would further say to const them, unless they need to modify the object.