www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Private variables accessible from outside class

reply Drobet <a b.c> writes:
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?
Aug 08 2019
next sibling parent reply Zoadian <no no.no> writes:
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
Aug 08 2019
parent reply Drobet <a b.c> writes:
On Thursday, 8 August 2019 at 15:53:13 UTC, Zoadian wrote:
 private means module private in D.
 see: https://dlang.org/spec/attribute.html#visibility_attributes
Then why does it in the tour say that it can only be "seen by Integer"? https://tour.dlang.org/tour/en/basics/classes
Aug 08 2019
parent ag0aep6g <anonymous example.com> writes:
On 08.08.19 18:03, Drobet wrote:
 Then why does it in the tour say that it can only be "seen by Integer"?
 https://tour.dlang.org/tour/en/basics/classes
That's an error in the tour.
Aug 08 2019
prev sibling next sibling parent matheus <matheus gmail.com> writes:
On Thursday, 8 August 2019 at 15:51:45 UTC, Drobet wrote:
 ...
 My question is if this is intended behavior, and if yes, why?
This is true if the class is inside the same module: "Private means that only members of the enclosing class can access the member, or members and functions in the same module as the enclosing class. Private members cannot be overridden."[1] Matheus. [1]https://wiki.dlang.org/Access_specifiers_and_visibility
Aug 08 2019
prev sibling next sibling parent Paul Backus <snarwin gmail.com> writes:
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.

 [...]

 My question is if this is intended behavior, and if yes, why?
For some context on why private works the way it does in D, take a look at this post on the official D blog: https://dlang.org/blog/2018/11/06/lost-in-translation-encapsulation/
Aug 08 2019
prev sibling parent Ethan <gooberman gmail.com> writes:
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
https://dlang.org/spec/attribute.html Section 8.4.2 of the spec reads: Symbols with private visibility can only be accessed from within the same module. Private member functions are implicitly final and cannot be overridden. If you were to put that Vector3 class in another module and import it, you'll find that private works as you expect. You'll find this ability very useful when you start using Uniform Function Call Syntax in your code. https://tour.dlang.org/tour/en/gems/uniform-function-call-syntax-ufcs
Aug 08 2019