www.digitalmars.com         C & C++   DMDScript  

D - changing protected variable

reply Yan <Yan_member pathlink.com> writes:
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;
}
}
Feb 09 2004
parent reply Roel Mathys <roel.mathys yucom.be> writes:
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
Feb 09 2004
parent reply Yan <Yan_member pathlink.com> writes:
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
Feb 09 2004
next sibling parent Roel Mathys <roel.mathys yucom.be> writes:
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
Feb 09 2004
prev sibling next sibling parent reply Roel Mathys <roel.mathys yucom.be> writes:
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
Feb 09 2004
parent reply Yan <Yan_member pathlink.com> writes:
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
Feb 09 2004
parent reply Roel Mathys <roel.mathys yucom.be> writes:
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
Feb 09 2004
parent reply Yan <Yan_member pathlink.com> writes:
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
Feb 10 2004
parent reply Hauke Duden <H.NS.Duden gmx.net> writes:
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
Feb 10 2004
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Hauke Duden wrote:

 Yan wrote:
[...]
 but with logical structure.
Agreed!
But how is the logical structure related to the physical layout of a project? From the friends section of the docs: | It makes sense that tightly related classes should be in the same module Should this be exended to the already available meaning of `package', i.e. modules that reside in the same directory? So long.
Feb 10 2004
parent reply Yan <Yan_member pathlink.com> writes:
In article <c0bmap$2lsi$2 digitaldaemon.com>, Manfred Nowak says...
Hauke Duden wrote:

 Yan wrote:
[...]
 but with logical structure.
Agreed!
But how is the logical structure related to the physical layout of a project?
IMHO logical structure should not be related to the physical layout. "project" is a substance controlled by IDE (for example) but not by compiler or linker.. Classes of the same module could be placed in several files (even on different computers..) Relation of logical structure and physical layout should be determined by programmer when he call compiler,linker or make.. May be i'm wrong but i see this concept as more universal. regards Yan
From the friends section of the docs:
| It makes sense that tightly related classes should be in the same module

Should this be exended to the already available meaning of `package', i.e.
modules that reside in the same directory?

So long.
Feb 11 2004
parent Manfred Nowak <svv1999 hotmail.com> writes:
Yan wrote:

[...]
 "project" is a substance controlled by IDE (for example) but not by
 compiler
[...]
 Classes of the same module could be placed in several files (even on
 different computers..)
What are the differences between your thinking in modules and projekts? And why s it not neccassary, that the used formal language is aware of that? So long.
Feb 11 2004
prev sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
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.
Feb 10 2004