www.digitalmars.com         C & C++   DMDScript  

D - Nesting oddity

reply Patrick Down <pat codemoon.com> writes:
I've compiled and run the following program in D.
It prints: Result = 14

class Foo
{
  private int Fred()
  {
    return 14;
  }
  
  class Bar // 1
  {
    int Barney()
    {
      return Fred(); // 2
    }
  }
}

int main(char[][] args)
{    
  Foo.Bar obj = new Foo.Bar(); // 3

  printf("Result = %d\n",obj.Barney());
    
  return 0;
  
}

Now this is a little weard because it expected one of three
compiler errors:

1. Can't nest classes
2. The C++ way: nested classes are just a way of scoping 
names. Function Fred is undefined in Barney.
Nested classes in C++ always seemed a little pointless 
to me.
3. The Java way: Nested classes are inner classes.
You can't create a Foo.Bar with having a Foo first or 
Bar needs to be static.  I like Java's inner classes.
May 07 2002
parent reply Patrick Down <pat codemoon.com> writes:
Patrick Down <pat codemoon.com> wrote in 
news:Xns9207B094D64E1patcodemooncom 63.105.9.61:

 2. The C++ way: nested classes are just a way of scoping 
 names. 
Sorry I ment to say that outer class is just a namespace for the inner one.
May 07 2002
parent "Walter" <walter digitalmars.com> writes:
I think you found a compiler bug. -Walter

"Patrick Down" <pat codemoon.com> wrote in message
news:Xns9207B1C2F6F86patcodemooncom 63.105.9.61...
 Patrick Down <pat codemoon.com> wrote in
 news:Xns9207B094D64E1patcodemooncom 63.105.9.61:

 2. The C++ way: nested classes are just a way of scoping
 names.
Sorry I ment to say that outer class is just a namespace for the inner one.
May 08 2002