Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 09, 2004 changing protected variable | ||||
---|---|---|---|---|
| ||||
As I suppose "protected" variable could not be changed outside the class. This program prints x=2 y=1 hmm... import std.c.stdio; int main(char[][] args) { Vector v =new Vector(); v.x = 2; printf("x=%d y=%d ",v.x,v.y); return 0; } class Vector { protected int x; protected int y; this() { // create vector x = 1; y = 1; } } |
February 09, 2004 Re: changing protected variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yan | Yan wrote:
> As I suppose "protected" variable could not be changed outside the class.
>
> This program prints
> x=2 y=1
>
> hmm...
>
>
> import std.c.stdio;
> int main(char[][] args) {
> Vector v =new Vector();
> v.x = 2;
> printf("x=%d y=%d ",v.x,v.y);
> return 0;
> }
> class Vector {
> protected int x;
> protected int y;
> this() { // create vector
> x = 1;
> y = 1;
> }
> }
>
>
hmm,
I think the access rules play outside the module wherein a variable is declared/defined.
This means inside a module (one file) the access restrictions are not enforced.
bye,
roel
btw: I did not verify this little story
|
February 09, 2004 Re: changing protected variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roel Mathys | In article <c08cli$2op9$1@digitaldaemon.com>, Roel Mathys says... > >Yan wrote: > >> As I suppose "protected" variable could not be changed outside the class. >> >> This program prints >> x=2 y=1 >> >> hmm... >> >> >> import std.c.stdio; >> int main(char[][] args) { >> Vector v =new Vector(); >> v.x = 2; >> printf("x=%d y=%d ",v.x,v.y); >> return 0; >> } >> class Vector { >> protected int x; >> protected int y; >> this() { // create vector >> x = 1; >> y = 1; >> } >> } >> >> > >hmm, >I think the access rules play outside the module wherein a variable is >declared/defined. > >This means inside a module (one file) the access restrictions are not enforced. Specification describes access rules in different way: "Protected means that only members of the enclosing class or any classes derived from that class can access the member." > >bye, >roel > >btw: I did not verify this little story |
February 09, 2004 Re: changing protected variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yan | Yan wrote:
> In article <c08cli$2op9$1@digitaldaemon.com>, Roel Mathys says...
>
>>Yan wrote:
>>
>>
>>>As I suppose "protected" variable could not be changed outside the class.
>>>
>>>This program prints
>>>x=2 y=1
>>>
>>>hmm...
>>>
>>>
>>>import std.c.stdio;
>>>int main(char[][] args) {
>>>Vector v =new Vector();
>>>v.x = 2;
>>>printf("x=%d y=%d ",v.x,v.y);
>>>return 0;
>>>}
>>>class Vector {
>>>protected int x;
>>>protected int y;
>>>this() { // create vector
>>>x = 1;
>>>y = 1;
>>>}
>>>}
>>>
>>>
>>
>>hmm,
>>I think the access rules play outside the module wherein a variable is declared/defined.
>>
>>This means inside a module (one file) the access restrictions are not enforced.
>
>
> Specification describes access rules in different way:
> "Protected means that only members of the enclosing class or any classes derived from that class can access the member."
>
>
>
>
>
>>bye,
>>roel
>>
>>btw: I did not verify this little story
>
>
>
yep,
sorry,
roel
|
February 09, 2004 Re: changing protected variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yan | Yan wrote:
> In article <c08cli$2op9$1@digitaldaemon.com>, Roel Mathys says...
>
>>Yan wrote:
>>
>>
>>>As I suppose "protected" variable could not be changed outside the class.
>>>
>>>This program prints
>>>x=2 y=1
>>>
>>>hmm...
>>>
>>>
>>>import std.c.stdio;
>>>int main(char[][] args) {
>>>Vector v =new Vector();
>>>v.x = 2;
>>>printf("x=%d y=%d ",v.x,v.y);
>>>return 0;
>>>}
>>>class Vector {
>>>protected int x;
>>>protected int y;
>>>this() { // create vector
>>>x = 1;
>>>y = 1;
>>>}
>>>}
>>>
>>>
>>
>>hmm,
>>I think the access rules play outside the module wherein a variable is declared/defined.
>>
>>This means inside a module (one file) the access restrictions are not enforced.
>
>
> Specification describes access rules in different way:
> "Protected means that only members of the enclosing class or any classes derived from that class can access the member."
>
>
>
>
>
>>bye,
>>roel
>>
>>btw: I did not verify this little story
>
>
>
although, the text beneath is stripped from the "Attributes" page:
-->
Protection Attribute
Protection is an attribute that is one of private, protected, public or export.
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. Private module members are equivalent to static declarations in C programs.
Protected means that only members of the enclosing class or any classes derived from that class can access the member. Protected module members are illegal.
Public means that any code within the executable can access the member.
Export means that any code outside the executable can access the member. Export is analogous to exporting definitions from a DLL.
<--
bye,
roel
|
February 09, 2004 Re: changing protected variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roel Mathys | Yes, from this page. In my view it clearly means that i can not
assign "protected" variable outside the class. Am I right?
Yan
>although, the text beneath is stripped from the "Attributes" page:
>
>-->
>Protection Attribute
>Protection is an attribute that is one of private, protected, public or
>export.
>
>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. Private module members are equivalent to static declarations in C programs.
>
>Protected means that only members of the enclosing class or any classes derived from that class can access the member. Protected module members are illegal.
>
>Public means that any code within the executable can access the member.
>
>Export means that any code outside the executable can access the member.
>Export is analogous to exporting definitions from a DLL.
><--
>
>
>bye,
>roel
|
February 09, 2004 Re: changing protected variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yan | Yan wrote:
> Yes, from this page. In my view it clearly means that i can not
> assign "protected" variable outside the class. Am I right?
> Yan
>
>
>>although, the text beneath is stripped from the "Attributes" page:
>>
>>-->
>>Protection Attribute
>>Protection is an attribute that is one of private, protected, public or export.
>>
>>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. Private module members are equivalent to static declarations in C programs.
>>
>>Protected means that only members of the enclosing class or any classes derived from that class can access the member. Protected module members are illegal.
>>
>>Public means that any code within the executable can access the member.
>>
>>Export means that any code outside the executable can access the member. Export is analogous to exporting definitions from a DLL.
>><--
>>
>>
>>bye,
>>roel
>
>
you are absolutely right,
but private members would be accessible from the module level.
my summary would be:
- public: accessible everywhere, including module level
- protected: accessible in downward class hierarchy
=> but derived classes can be in other modules
- private: from module level, including other (derived) classes in this module (and "finalizes" a specific class member when used)
=> one can't compare private and protected, they are two different concepts, I think this one could be tricky for C++ users
bye,
roel
|
February 10, 2004 Re: changing protected variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Roel Mathys | In article <c08qin$10oi$1@digitaldaemon.com>, Roel Mathys says... > >Yan wrote: >> Yes, from this page. In my view it clearly means that i can not >> assign "protected" variable outside the class. Am I right? >> Yan >> >> >>>although, the text beneath is stripped from the "Attributes" page: >>> >>>--> >>>Protection Attribute >>>Protection is an attribute that is one of private, protected, public or >>>export. >>> >>>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. Private module members are equivalent to static declarations in C programs. >>> >>>Protected means that only members of the enclosing class or any classes derived from that class can access the member. Protected module members are illegal. >>> >>>Public means that any code within the executable can access the member. >>> >>>Export means that any code outside the executable can access the member. >>>Export is analogous to exporting definitions from a DLL. >>><-- >>> >>> >>>bye, >>>roel >> >> My uderstanding of what describes specification is like that: (Table1) Out of module Module Class Subclass public + + + + private - - + - protected - - + + > >you are absolutely right, >but private members would be accessible from the module level. > That means: (Table2) Out of module Module Class Subclass public + + + + private - - + - protected - __+__ + + >my summary would be: >- public: accessible everywhere, including module level >- protected: accessible in downward class hierarchy > => but derived classes can be in other modules >- private: from module level, including other (derived) classes in this module (and "finalizes" a specific class member when used) That is means: (Table3) Out of module Module Class Subclass public + + + + private - + + +(only in Module) protected - - + +(also in other Modules) I think that LOGICAL structure of the program is more important, rather than PHISICAL distribution of classes in source files. So, imho Module should not be or equial to file. Many classes in different files could represent ONE module. Accessibility should not be connected with file structure of the project, but with logical structure. That is why I like Table1. regards, Yan > >=> one can't compare private and protected, they are two different concepts, I think this one could be tricky for C++ users > >bye, >roel |
February 10, 2004 Re: changing protected variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yan | Yan wrote:
> I think that LOGICAL structure of the program is more important,
> rather than PHISICAL distribution of classes in source files.
> So, imho Module should not be or equial to file.
> Many classes in different files could represent ONE module.
> Accessibility should not be connected with file structure of
> the project, but with logical structure.
Agreed!
Hauke
|
February 10, 2004 Re: changing protected variable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yan | Yan wrote:
[...]
> Specification describes access rules in different way:
> "Protected means that only members of the enclosing class or any classes
> derived from that class can access the member."
Look at the friends section:
| In D, friend access is implicit in being a member of the same module.
So long.
|
Copyright © 1999-2021 by the D Language Foundation