3 ms·
> To hide implementation details from class users. Yes, that's what private does. My question was more why you should do that. > the interface can stay the sa
by robalni 4y ago
> To hide implementation details from class users.
Yes, that's what private does. My question was more why you should do that.
> the interface can stay the same even though you could completely re-do the guts.
So is the reason to use private that it's a way to tell programmers to not use something in too many places because that will make it hard to change in all those places if you need to change the private member?
- jacquesm 4y agoNo, it says 'this is not meant for you to change from the outside'. It's an implementation detail, not part of the interface and any references to it are bound to break without notice, warning or acceptance of the consequences. Think of the interface to your class as a contract: this is how this class works, from now until eternity (or until the next breaking change ;) ). Relying on implementation details breaks that contract, the contract is on the interface not on the guts. It won't be 'hard to change' it may be impossible to change. Because someone that relies on your class may not have access to the code of the other class!
- robalni 4y ago> any references to it are bound to break Do you mean that if someone changes a class member, they would not change in all places that use that class member? Why not? > someone that relies on your class may not have access to the code As I wrote in the clarification, I'm only interested in cases where everyone has access to all the code.