Private variables accessible from outside class

Zoadian no at no.no
Thu Aug 8 15:53:13 UTC 2019


On Thursday, 8 August 2019 at 15:51:45 UTC, Drobet wrote:
> I'm having a weird issue, where after defining my classes 
> variables as private, they can still be modified and looked at 
> from the outside. That leads to this code compiling with no 
> issues.
>
> import std.stdio;
>
> class Vector3
> {
>     this(double _x = 0.0, double _y = 0.0, double _z = 0.0)
>     {
> 	x = _x;
>         y = _y;
>         z = _z;
>     }
>
>     private:
>         double x, y, z;
> }
>
> int main()
> {
>     Vector3 vec = new Vector3(5, 5, 5);
>     vec.x = 10;
>     writeln(vec.x);
>
>     getchar();
>
>     vec.destroy();
>     return 0;
> }
>
> My question is if this is intended behavior, and if yes, why?

private means module private in D.
see: https://dlang.org/spec/attribute.html#visibility_attributes



More information about the Digitalmars-d-learn mailing list