D - Problem using private and public attributes in classes
- Julio Jiménez (34/34) Feb 24 2004 compiling this example:
- Marko Nikolic (5/7) Feb 24 2004 Because they are private at the module not class level. If you
- =?ISO-8859-1?Q?Julio_Jim=E9nez?= (5/16) Feb 24 2004 Thanks. Attributes at module level!. Now i can understand it. he he :-)
- Marko Nikolic (8/26) Feb 24 2004 Not all (I haven't tried protected) according to documentation (see
- =?ISO-8859-1?Q?Julio_Jim=E9nez?= (10/18) Feb 24 2004 Well. I have tried it and using protected is equal to using private. The...
compiling this example: class Foo { private: int a; int b; float c; public: this() { c = 3.14; a = 1; printf("constructor called\n"); } ~this() { printf("Destructor called\n"); } } int main() { Foo E = new Foo(); printf("a=%d b=%d c=%4.2f\n", E.a, E.b, E.c); return 0; } the program compiles and run ok, getting: constructor called a=1 b=0 c=3.14 Destructor called but the question is why i can access to private members of the class? what is wrong? is a bug? i'm using dmd v0.79 for linux regards Julio Jiménez
Feb 24 2004
Julio wrote:but the question is why i can access to private members of the class? what is wrong? is a bug?Because they are private at the module not class level. If you import this module in another one private fields won't be accessible. Regards, Marko
Feb 24 2004
Marko Nikolic wrote:Julio wrote:Thanks. Attributes at module level!. Now i can understand it. he he :-) the same class in a separate module run fine. Regards, Juliobut the question is why i can access to private members of the class? what is wrong? is a bug?Because they are private at the module not class level. If you import this module in another one private fields won't be accessible. Regards, Marko
Feb 24 2004
Julio Jiménez wrote:Marko Nikolic wrote:Not all (I haven't tried protected) according to documentation (see under Attributes->Protection Attributes. There is clearly stated that private members of class are accessible to all member functions of enclosing module. But reading only documentation protected members should not be accessible in whole module. Regards, MarkoJulio wrote:Thanks. Attributes at module level!. Now i can understand it. he he :-) the same class in a separate module run fine.but the question is why i can access to private members of the class? what is wrong? is a bug?Because they are private at the module not class level. If you import this module in another one private fields won't be accessible. Regards, Marko
Feb 24 2004
Marko Nikolic wrote:Not all (I haven't tried protected) according to documentation (see under Attributes->Protection Attributes. There is clearly stated that private members of class are accessible to all member functions of enclosing module. But reading only documentation protected members should not be accessible in whole module. Regards, MarkoWell. I have tried it and using protected is equal to using private. The documentation must say: Private or Protected members of the enclosing class can access the member, or members and functions in the same module as the enclosing class. Of course Protected members means that only members of the enclosing class or any classes derived from that class can access the member (in any module). Regards, Julio
Feb 24 2004