Private classes?

Regan Heath regan at netmail.co.nz
Tue Aug 21 02:07:23 PDT 2007


Nathan Reed wrote:
> Hello,
> 
> I'm not sure if this is the appropriate mailing list on which to post
> this, so please tell me if it's not.

digitalmars.D.learn might be better in this case.  Once you've used D 
more you'll get a feel for which list to post in in any given case.

> I have been playing around with D in the last few days and found that
> there doesn't seem to be any way to hide a class in a module using
> 'private'.  For example, using the DMD version 2.003, the following
> compiles without errors:
> 
> module A; private class Foo { } ------------ module B; import A; ... 
> auto f = new Foo;    // expected: error, Foo is private

Try this:

[pclassA.d]
module pclassA;

class A
{
	private this()
	{
	}
}

[pclassB.d]
module pclassB;
import pclassA;

void main()
{
	A a = new A();
}

> If A has a privately declared *variable* and I try to access it from
> B, I get the expected error.
> 
> The same thing happens with inner classes; the following compiles
> without errors:
> 
> class Foo { private class Bar { } } ... auto f = new Foo; auto b =
> f.new Bar;   // expected: error, Bar is private

Assuming these are in different modules, as in your first example.  You 
can apply private to the constructor for Bar to prevent construction.

> Now, I can't find anyplace in the D spec where it says that 'private'
> ought to work on classes...so I suppose this isn't technically a bug.
> However, it seems like the sort of thing that one ought to be able to
> do.

AFAIK you cannot apply 'private' to a class.

Regan



More information about the Digitalmars-d mailing list