0
Why does std::string have a method for calculating the length, instead of just a property?
I mean, std::string::length() would have to iterate through the whole string to find its length. Why didn't the devs make a length property to solve this issue?
8 odpowiedzi
+ 3
The C++ standard requires the length() and size() methods to have constant time complexity. That implicitly necessitates the existence of such a length property, the two methods just act as getters for it.
+ 1
As you said, the length property should not be modifiable from the outside, but it obviously needs to be updated internally by different methods. Therefore, one point of a getter can be to limit the access to a necessarily mutable property in a read-only manner for the user.
Furthermore, a string implementation can be quite complex, with various optimizations in its design. For example, if you implement short string optimization, you get to a point where you need internal logic to decide what to return as the length value, meaning a function is basically necessary. It is hard to explain here, but this article might be worth a read:
https://joellaity.com/2020/01/31/string.html
Regarding your last point, compilers have the ability to inline function calls (the call to the function is replaced by the function body itself, essentially getting rid of the jump). I think there is less overhead than what you imagine.
+ 1
You aren't really wrong, it just depends on how the class is implemented. The case you describe, where the class holds only a pointer to the dynamically allocated buffer and the size, is probably the simplest string implementation. What I wanted to convey through the link is that implementations can get a lot crazier than that, however.
For example, a slightly more advanced one would hold not one but two size properties, where one represents the size of the allocated buffer, and one the size of the actual stored string. Here, you often allocate more space than potentially necessary in order to reduce the number of reallocations when the string growths (the short string optimization from the article is a more advanced variant of this).
Essentially, the length of the string is stored somehow in the class, but because of optimization tricks and whatnot, it does not always have to be a single property, hence the access through a function.
+ 1
Maybe I didn't understand correctly, but I think you have a little misunderstanding. Short strings, in the way I talked about earlier, is an internal mechanism of the string class. You as the user have no way of knowing whether this implementation technique is used, and if yes, when the string is in short mode (unless your library implementation is documented to that point or you have access to the source code).
0
@Shadow Thank you for the response, but what's the point of using a getter in the first place? I mean, it makes total sense that the length property is something that shouldn't be modified from outside. Making a separate getter method slows the whole process down.
0
Shadow To be honest the thread you linked went straight over my head, but thanks for that.
P.S. Am I still wrong to assume that length() is just a getter method for an in-lying length property? I suppose that the string would've to have two properties, one for the data length and one for the array capacity.
0
Shadow I see, so if I had to create an array of strings that wouldn't exceed 100 characters, I'd be better off with short strings right?
0
Shadow Damn, I would've to read more about it. Sorry for being such a noob