3 ms·
> I would say private is a way to tell the user to not touch the variable, or to use an accessor function that has validation to prevent entering invalid state.
by robalni 4y ago
> I would say private is a way to tell the user to not touch the variable, or to use an accessor function that has validation to prevent entering invalid state.
That's what I meant, that it's a way to tell the programmer that you may not need to touch this variable directly because there are already functions that do that so please look at those functions to see if there is one that does what you need. Private then makes sure that all functions that touch the variable are added to the list because you can't use a private variable in a function not in the list. The list I'm talking about is the function declarations/definitions in the class.
This C++ code doesn't compile because `number` is private by default:
class A {
int number;
};
void zero(A &a) {
a.number = 0;
}
But this works because now `zero` is added to a list of functions inside the class and that way the function is easier to find for someone who wants to modify `number` (assuming the function and the class were not already defined right next to each other):
class A {
int number;
void zero();
};
void A::zero() {
number = 0;
}
So the idea is that private can help with reusing code because it makes sure that all functions that modify the variable are listed in the class, which increases the chance of them being used.
Otherwise, what's the difference between my two examples that makes the second one better than the first one (other than that the first one doesn't compile)? The only difference is that the function has been added to a list inside the class and got a slightly different name.
- xboxnolifes 4y agoI think the disconnect between me and you here is that you're looking at the the "user" ("programmer" in your comment) in our hypotheticals as a person who is going to implement the class. In my mind, they are the person who is using the class. They will not be implementing unimplemented function signatures. In my mind, in my comments, they are library users. I'm thinking of someone who is using std::vector and using std::vector::pop_back instead of trying to access the underlying container, grabbing the value, deleting the value, and setting the private variables to match the new state. > Otherwise, what's the difference between my two examples that makes the second one better than the first one (other than that the first one doesn't compile)? The only difference is that the function has been added to a list inside the class and got a slightly different name. The second one is not better than the first one. I would say it's a completely pointless approach to a problem, and would be better served by just making number public. Leaving an empty zero function signature around so someone can come by and fill it in to do what they want serves absolutely no useful purpose at all here. If zeroing out the number property of class A is useful for A, then it should be an already implemented function. That being said, the example given, in my eyes, is not useful. What is "number"? Why is it private? What does "A" do? What are it's expected states? What are it's failure conditions? If A cannot enter an invalid state by changing number, there is no reason for it to be private. As I previously said, private exists to prevent objects from entering invalid states from external influence. If A#number has no invalid state, then it need not be private.
- 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.