Using closure in function scope to make "real" private class members

Dom Disc dominikus at scherkl.de
Sun Jun 5 19:41:36 UTC 2022


On Saturday, 4 June 2022 at 14:40:10 UTC, zjh wrote:
> For me, it would be better ,if I could encapsulate at `class 
> level`.In any case, I don't want the `outside` to affect `my 
> class inside`.

C++ "class level privacy" needs to have "friends", else you 
cannot create top-level functions that have access to the private 
members.
But these "friends" destroy the whole purpose of "private":
Anybody can declare friends, without even touching the file where 
the class is defined, and thereby access things that shouldn't 
even be visible to them (implementation details or even security 
relevant things)!
In D you need to change the file where a class is defined if you 
need to access its privates - this is much easier visible and in 
a larger team can't be hidden from the person responsible for 
that class.

forkit wrote:
> my argument is not favouring one or the other. It's favouring 
> the
> 'option' to have both.

But C++ private is just working by convention - it doesn't 
enforce anything (because it can be circumvented with friends, 
same problem as with const which can be circumvented with 
mutable).
By adding this broken thing to D it will be less save and can't 
guarantee any privacy anymore, just like with C++ now.


More information about the Digitalmars-d mailing list