www.digitalmars.com         C & C++   DMDScript  

D - Problem using private and public attributes in classes

reply Julio Jiménez <Julio_member pathlink.com> writes:
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
parent reply Marko Nikolic <markoni69 verat.net> writes:
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
parent reply =?ISO-8859-1?Q?Julio_Jim=E9nez?= <julio inicia.es> writes:
Marko Nikolic wrote:
 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

Thanks. Attributes at module level!. Now i can understand it. he he :-) the same class in a separate module run fine. Regards, Julio
Feb 24 2004
parent reply Marko Nikolic <markoni69 verat.net> writes:
Julio Jiménez wrote:

 Marko Nikolic wrote:
 
 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

Thanks. Attributes at module level!. Now i can understand it. he he :-) the same class in a separate module run fine.

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, Marko
Feb 24 2004
parent =?ISO-8859-1?Q?Julio_Jim=E9nez?= <julio inicia.es> writes:
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,
 Marko

Well. 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