D - Protection attributes - Inconsistency?
- Sarat Venugopal (67/67) Nov 07 2003 Hello all,
- Walter (5/69) Nov 11 2003 That's a compiler bug.
Hello all, I have scanned the newsgroup messages and the documentation, but was unable to get definitive info on the following: 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. 2) Can protection attributes be applied to members of a struct? Apparently not. 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. 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; }
Nov 07 2003
"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; }
Nov 11 2003