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

forkit forkit at gmail.com
Mon Jun 6 01:05:44 UTC 2022


On Sunday, 5 June 2022 at 19:41:36 UTC, Dom Disc wrote:
> 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.

What I like about C++ friend concept, is that you explicately 
know who is a friend and who isn't. It's declared right there in 
the class.

In a D module, everything could be a friend. Everything! You'll 
have to look at ALL the code in the module to determine that - as 
opposed to C++, where you can find this information right there 
in the class declaration.

Imagine if everyone in your street was your friend, and they 
could come around willy nilly and do as they please, in your 
nice, encapsulated home.

Now, to be fair, it's nice to have a friend around. But all the 
time!!! Doing whatever the %F#V they want!!!



More information about the Digitalmars-d mailing list