how to make private class member private

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue Mar 13 01:39:13 UTC 2018


On Tuesday, March 13, 2018 01:12:15 psychoticRabbit via Digitalmars-d-learn 
wrote:
> I cannot get my head around, why private is not private, in D.
>
> How do I make a private member, private?
>
> -----
> module test;
>
> import std.stdio;
>
> void main()
> {
>      myClass c = new myClass();
>      c.myPrivateClassMember= "wtf";
>      writeln(c.myPrivateClassMember);
> }
>
> class myClass
> {
>      private string myPrivateClassMember; // private does not mean
> private anymore??
> }
>
> ------

private is private to the module, not the class. There is no way in D to
restrict the rest of the module from accessing the members of a class. This
simplification makes it so that stuff like C++'s friend are unnecessary. If
your class in a separate module from main, then main won't be able to access
its private members.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list