3 ms·
> They will not be implementing unimplemented function signatures. In my mind, in my comments, they are library users. I clarified in the question that I'm onl
by robalni 4y ago
> They will not be implementing unimplemented function signatures. In my mind, in my comments, they are library users.
I clarified in the question that I'm only asking about situations where everyone has access to all of the code. So are we assuming that the organization is structured so that only one person/team works on a class and that no-one else is supposed to touch it? Or why would the class user not write a new function that accesses the member variables? Is it because the variables are private, which tells them to look at existing functions, just like I described?
> That being said, the example given, in my eyes, is not useful.
Ok, let's do a more useful example, a string class, where we assume that `len` may never be negative (and we assume that there are other string functions defined in the same way as `truncate` to make the class more useful; I just didn't want to write them in this example).
Code 1:
class Str {
public:
char *data;
int len;
};
void truncate(Str *s) {
s->len = 0;
}
Code 2:
class Str {
char *data;
int len;
public:
void truncate();
};
void Str::truncate() {
len = 0;
}
What's the difference between these two and why is code 2 better than code 1? As I currently understand it, the difference is that in code 2 the truncate function has been forced (because of private member variables) to be added to a list of functions inside the class, which makes it easier to find the function for someone who wants to truncate the string and therefore it reduces the risk that someone implements that function again when it already exists.