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

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Thu Jun 9 13:15:14 UTC 2022


On Thursday, 9 June 2022 at 13:05:46 UTC, Max Samukha wrote:
> 'friend' is abominable for a different reason - it breaks 
> encapsulation by exposing the entire state instead of selected 
> members, and D is no different in this respect.

Not necessarily. You can put all the members you want to expose 
in a separate C++ class inside the other class and only expose 
this one.

You usually don't want to make another class a friend, just some 
specified functions.

The tools for improved encapsulation are there even if most C++ 
programmers don't care enough to use them.

And I guess that also is the situation for D, most D users care 
more about convenience than strictness. You see this in D code 
bases by the tendency to «abuse» string mixins!

At the end of the day, culture has a massive impact…



More information about the Digitalmars-d mailing list