Jump to page: 1 2
Thread overview
changing protected variable
Feb 09, 2004
Yan
Feb 09, 2004
Roel Mathys
Feb 09, 2004
Yan
Feb 09, 2004
Roel Mathys
Feb 09, 2004
Roel Mathys
Feb 09, 2004
Yan
Feb 09, 2004
Roel Mathys
Feb 10, 2004
Yan
Feb 10, 2004
Hauke Duden
Feb 10, 2004
Manfred Nowak
Feb 11, 2004
Yan
Feb 11, 2004
Manfred Nowak
Feb 10, 2004
Manfred Nowak
February 09, 2004
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
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
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
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
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
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
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
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
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
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.
« First   ‹ Prev
1 2