Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
May 22, 2004 Protected constructors? | ||||
---|---|---|---|---|
| ||||
Is it just me, or should this not compile? (it does) ---------- class Foo { protected this() { } } int main( char[][] args ) { Foo f = new Foo; return 0; } ---------- The D specs say that "Protected means that only members of the enclosing class or any classes derived from that class can access the member." However, /something/ outside the class is calling Foo.this()... I'm writing my own singleton mixin, and I'm trying to keep the rest of the world from instantiating any singletons. Is this not the right way to approach the problem? Michael Coupland |
May 22, 2004 Re: Protected constructors? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael Coupland | Foo and main are in same module.
So they are friend.
Michael Coupland wrote:
> Is it just me, or should this not compile? (it does)
>
> ----------
>
> class Foo
> {
> protected this() { }
> }
>
> int main( char[][] args )
> {
> Foo f = new Foo;
> return 0;
> }
>
> ----------
>
> The D specs say that "Protected means that only members of the enclosing class or any classes derived from that class can access the member." However, /something/ outside the class is calling Foo.this()...
>
> I'm writing my own singleton mixin, and I'm trying to keep the rest of the world from instantiating any singletons. Is this not the right way to approach the problem?
>
> Michael Coupland
|
May 22, 2004 Re: Protected constructors? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Genki Takiuchi | Yes, and if I had made the constructor /private/ then I would understand; however the constructor is protected... The documentation implies that protected (in D) is just a more restricted version of private, which prevents /anything/ but derived classes from reaching in. Or am I misunderstanding something?
Michael
Genki Takiuchi wrote:
> Foo and main are in same module.
> So they are friend.
>
> Michael Coupland wrote:
>
>> Is it just me, or should this not compile? (it does)
>>
>> ----------
>>
>> class Foo
>> {
>> protected this() { }
>> }
>>
>> int main( char[][] args )
>> {
>> Foo f = new Foo;
>> return 0;
>> }
>>
>> ----------
>>
>> The D specs say that "Protected means that only members of the enclosing class or any classes derived from that class can access the member." However, /something/ outside the class is calling Foo.this()...
>>
>> I'm writing my own singleton mixin, and I'm trying to keep the rest of the world from instantiating any singletons. Is this not the right way to approach the problem?
>>
>> Michael Coupland
|
May 22, 2004 Re: Protected constructors? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael Coupland | This may be a bug... Walter had written in old message: "Sarat Venugopal" <sarat@removeme.huelix.com> wrote in message news:bogb7f$1cc1$1@digitaldaemon.com... >> 1) Are constructors special in that they do not honor protection >> attributes? Apparently, it doesn't matter whether the ctor is private - >> the class can be instantiated regardless. That's a compiler bug. >> 2) Can protection attributes be applied to members of a struct? >> Apparently not. That should work. >> 3) Can a class definition be protected? i.e. I would like a class to be >> visible only within its module. >> >> private class Foo {...} >> >> The compiler accepts this but the visbility is public. That's a bug. >> 4) "protected" modifier on a module member is illegal according to the >> docs. But the code compiles all the same and works as if the memeber >> were public. Shouldn't the compiler generate an error here? Of course, >> there is no valid reason to do this, but since there are no warnings >> issued by the compiler, it would be nice to flasg the illegal ones as >> errors. >> >> Thoughts? >> >> Cheers, >> Sarat Venugopal >> >> Some code to illustrate: >> >> protect.d >> -------------------------------------------------------------------------- >> import std.c.stdio; >> >> class CTest >> { >> private: >> this() { printf("Private default ctor\n"); } >> >> this(int x) { printf("Private int arg ctor\n"); x_ = x; } >> >> public: >> // Compiler error if this is private >> void Print() { printf("I would like some privacy here\n"); } >> >> private: >> int x_; >> } >> >> // Are protection attributes valid in a struct? >> struct STest >> { >> private: >> int x_ = -10; >> int y_ = 100; >> } >> >> // This is okay >> public STest sPub; >> >> // This passes - Suppposed to be illegal >> // The line below is quite dumb - just to illustrate >> protected STest sProt; >> >> // Okay, this is private - compiler error >> // private STest sPriv; >> >> -------------------------------------------------------------------------- >> >> main.d >> -------------------------------------------------------------------------- >> import protect; >> >> int main() >> { >> // Test ctor is declared private >> CTest t = new CTest(); >> t.Print(); >> >> // x_ and y_ are declared private, but visible >> printf("Public struct (x,y): (%d, %d)\n", sPub.x_, sPub.y_); >> >> // sProt is a protected module member - should be illegal? >> printf("Protected struct (x,y): (%d, %d)\n", sProt.x_, sProt.y_); >> >> // Okay, module level privacy is clearly honored >> //printf("Private struct (x,y): (%d, %d)\n", sPriv.x_, sPub.y_); >> >> return 0; >> } >> |
May 22, 2004 Re: Protected constructors? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Genki Takiuchi | Ah hah. Thanks very much.
Michael
Genki Takiuchi wrote:
> This may be a bug...
>
> Walter had written in old message:
>
<snip>
|
May 22, 2004 Re: Protected constructors? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael Coupland | "Michael Coupland" <mcoupland@hmc.edu> wrote in message news:c8mkkl$2bmp$1@digitaldaemon.com... > Yes, and if I had made the constructor /private/ then I would understand; however the constructor is protected... The documentation implies that protected (in D) is just a more restricted version of private, which prevents /anything/ but derived classes from reaching in. Or am I misunderstanding something? I think you will find that private is more restrictive than protected Phill. |
Copyright © 1999-2021 by the D Language Foundation