www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Protection attributes does not work in accordance with docs

reply nail <nail_member pathlink.com> writes:
Hi all,

Here is the quote from http://www.digitalmars.com/d/attribute.html:

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.

So the question - why following code does not compiles:

module test;

class A
{
public class Nested
{
private:
uint someMember;
}

this()
{
nest = new Nested();
}

void foo()
{
nest.someMember = 10; // error here
}

private:
Nested nest;
}

int main ( char [] [] args )
{
A a = new A();
a.foo();

return 0;	
}

Error is: class rame.test.A.Nested member someMember is not accessible
Jan 26 2005
next sibling parent reply nail <nail_member pathlink.com> writes:
In article <ct7udp$1dor$1 digitaldaemon.com>, nail says...
Hi all,

Here is the quote from http://www.digitalmars.com/d/attribute.html:

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.

So the question - why following code does not compiles:

module test;

class A
{
public class Nested
{
private:
uint someMember;
}

this()
{
nest = new Nested();
}

void foo()
{
nest.someMember = 10; // error here
}

private:
Nested nest;
}

int main ( char [] [] args )
{
A a = new A();
a.foo();

return 0;	
}

Error is: class rame.test.A.Nested member someMember is not accessible

No replies. Hm... Is this question so stupid? I can not understand is it a bug in dmd or my misunderstood.
Jan 28 2005
next sibling parent "Ben Hinkle" <ben.hinkle gmail.com> writes:
 No replies. Hm...
 Is this question so stupid? I can not understand is it a bug in dmd or my
 misunderstood.

I think it is a compiler bug. It works if the class Nested is moved to the top level.
Jan 28 2005
prev sibling parent reply David L. Davis <SpottedTiger yahoo.com> writes:
In article <ctd3fi$240l$1 digitaldaemon.com>, nail says...
No replies. Hm...
Is this question so stupid? I can not understand is it a bug in dmd or my
misunderstood.

nail: If you comment out the "private:" in the Nested class it seems to work just fine...but since you should be able to place the "private" keyword here if you'd like to, I'd say it's a bug. Also below I've added a fooprint function to verify that I am indeed getting the correct value back. # module test; # private import std.stdio; # # class A # { # # public class Nested # { # //private: # uint someMember; # } # # this() # { # nest = new Nested(); # } # # void foo() # { # nest.someMember = 10; // error here # } # # void fooprint() # { # writefln( "nest.someMember=%d", nest.someMember ); # } # # private: # Nested nest; # } # # int main ( char [] [] args ) # { # A a = new A(); # a.foo(); # a.fooprint(); # # return 0; # } output: C:\dmd>bin\dmd test.d C:\dmd\bin\..\..\dm\bin\link.exe test,,,user32+kernel32/noi; C:\dmd>test nest.someMember=10 David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jan 28 2005
parent reply nail <nail_member pathlink.com> writes:
nail: If you comment out the "private:" in the Nested class it seems to work
just fine...

If I'll comment it foo would be avaible from other modules. That's not what I want. So if it is a bug, hope Walter will keep it in mind.
Jan 28 2005
parent David L. Davis <SpottedTiger yahoo.com> writes:
In article <ctdt81$2k1$1 digitaldaemon.com>, nail says...
nail: If you comment out the "private:" in the Nested class it seems to work
just fine...

If I'll comment it foo would be avaible from other modules. That's not what I want. So if it is a bug, hope Walter will keep it in mind.

nail: With a second thought, this may not be an error after all, but a scoping issue. Since the nested class is hiding it's private members from the parent class (I think this is normal), and since the nested class can't be inherted...you can't use the "protected" keyword, which only leaves public. Below I've added getValue() and setValue() methods to the nested class, which I think does what you're looking for. # module test; # private import std.stdio; # # class A # { # public class Nested # { # private uint someMember; # # void setValue( in uint ui ) # { # someMember = ui; # } # # uint getValue() # { # return someMember; # } # } # # this() # { # nest = new Nested(); # } # # void foo() # { # //nest.someMember = 10; // error here # nest.setValue( 10 ); # } # # void fooprint() # { # writefln( "nest.getValue()=%d", nest.getValue() ); # } # # private: # Nested nest; # } # # int main ( char [] [] args ) # { # A a = new A(); # a.foo(); # a.fooprint(); # # // This did work when "uint someMember" was public, # // but with the new changes it doesn't. Which is what you # // wanted...right? # //writefln( "a.nest.someMember=%d", a.nest.someMember ); # # return 0; # } David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
Jan 28 2005
prev sibling parent "Carlos Santander B." <csantander619 gmail.com> writes:
nail wrote:
 Hi all,
 
 Here is the quote from http://www.digitalmars.com/d/attribute.html:
 
 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.
 

Is it me, or this is ambiguous? It should be one thing, or the other thing, but whatever it is, it should be explicitly stated. Or maybe I'm just mis-reading it.
 So the question - why following code does not compiles:
 
 module test;
 
 class A
 {
 public class Nested
 {
 private:
 uint someMember;
 }
 
 this()
 {
 nest = new Nested();
 }
 
 void foo()
 {
 nest.someMember = 10; // error here
 }
 
 private:
 Nested nest;
 }
 
 int main ( char [] [] args )
 {
 A a = new A();
 a.foo();
 
 return 0;	
 }
 
 Error is: class rame.test.A.Nested member someMember is not accessible
 
 

I suggest you to post a bug report in the bugs ng. _______________________ Carlos Santander Bernal
Jan 31 2005