Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 24, 2004 Problem using private and public attributes in classes | ||||
---|---|---|---|---|
| ||||
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 |
February 24, 2004 Re: Problem using private and public attributes in classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Julio Jiménez | 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
|
February 24, 2004 Re: Problem using private and public attributes in classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marko Nikolic | 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
|
February 24, 2004 Re: Problem using private and public attributes in classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Julio Jiménez | 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
|
February 24, 2004 Re: Problem using private and public attributes in classes | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marko Nikolic | 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
|
Copyright © 1999-2021 by the D Language Foundation