3 ms·
From your other replay: > Is the reason to make those variables private then to tell the programmer that there is a function that changes them and that you hav
by xboxnolifes 4y ago
From your other replay:
> Is the reason to make those variables private then to tell the programmer that there is a function that changes them and that you have to look through all the functions of the class before you touch them to see if there is a function that does what you need?
I'm not sure I understand the question.
A private property is a property that a class user should have no reason to want to directly write to. Or more accurately, it's a variable that directly writing to could invalidate the state of the object. They are intermediate values or implementation details that define the state of the object. Reading them should be fine, but allowing unrestricted writing is dangerous. If there is reason to directly change the value, there should be a public method that includes validation to prevent the user from invalidating the object.
On the other hand, private methods exist for code re-use convenience purposes. You could just as easily only have public methods and write all logic inside of them. But a lot of the time you have pieces of code that could be reused between methods, but on its own doesn't guarantee the object to be in a valid state at the beginning or end of the call. So, you make it private. It only exists as a means of avoiding code duplication and possibly improving readability.
Private methods may also have value in inheritance / abstract classes, but I'm focusing on invalid state.
- robalni 4y ago> A private property is a property that a class user should have no reason to want to directly write to. What the user of a class has reason to do depends on what they need to get done. Maybe there is a function that already does what they need to do or maybe there isn't. So the choice is: call a function or write code that directly accesses the variable. This is a choice you would have both for private and public variables. But people say that they make things private because the class user should use already existing functions instead of touching the variable directly if there is already a function that does it. So does that mean that private is a way to tell the class user that there are functions that touch this variable, to increase the chances that the class user finds and uses them?
- xboxnolifes 4y ago> So does that mean that private is a way to tell the class user that there are functions that touch this variable, to increase the chances that the class user finds and uses them? 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. At this point I feel like I'm just repeating myself, as I can't imagine a concrete example of what you're asking. Could you give a hypothetical, or better yet real, example of what you're thinking of here? I can't think of any at all. I felt that the example by Jtsummers was a great illustration.
- 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 ago