| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
October 05, 2013 private attribute don't function? | ||||
|---|---|---|---|---|
| ||||
import std.stdio;
class A
{
private int j = 1;
private
{
int k = 2;
}
private:
int i = 0;
}
void main()
{
A a = new A;
writeln(a.i);
writeln(a.j);
writeln(a.k);
}
It compiles and output 0 1 2 repectively.
| ||||
October 05, 2013 Re: private attribute don't function? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Zhouxuan | On Saturday, 5 October 2013 at 15:19:34 UTC, Zhouxuan wrote:
> import std.stdio;
>
> class A
> {
> private int j = 1;
>
> private
> {
> int k = 2;
> }
>
> private:
> int i = 0;
> }
>
> void main()
> {
> A a = new A;
> writeln(a.i);
> writeln(a.j);
> writeln(a.k);
> }
In D, "private" means "private to the current module". If you try accessing the fields from another module, you'll see that it doesn't work.
In the future, please consider posting similar question in digitalmars.D.learn.
David
| |||
October 05, 2013 Re: private attribute don't function? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Zhouxuan | http://dlang.org/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. Private members cannot be overridden. Private module members are equivalent to static declarations in C programs. " In D, private is only hidden to different modules. If it is in the same module, they can see it anyway. | |||
October 05, 2013 Re: private attribute don't function? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to David Nadlinger | On Saturday, 5 October 2013 at 15:21:46 UTC, David Nadlinger wrote:
> On Saturday, 5 October 2013 at 15:19:34 UTC, Zhouxuan wrote:
>> import std.stdio;
>>
>> class A
>> {
>> private int j = 1;
>>
>> private
>> {
>> int k = 2;
>> }
>>
>> private:
>> int i = 0;
>> }
>>
>> void main()
>> {
>> A a = new A;
>> writeln(a.i);
>> writeln(a.j);
>> writeln(a.k);
>> }
>
> In D, "private" means "private to the current module". If you try accessing the fields from another module, you'll see that it doesn't work.
>
> In the future, please consider posting similar question in digitalmars.D.learn.
>
> David
Okay, got it, thank you!
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply