dotgnu-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [DotGNU]How do you simulate an inner class in C#?


From: Thong \(Tum\) Nguyen
Subject: RE: [DotGNU]How do you simulate an inner class in C#?
Date: Sun, 25 May 2003 18:53:59 +1200

> -----Original Message-----
> From: Gopal V [mailto:address@hidden
> Sent: Sunday, 25 May 2003 6:14 p.m.
> To: Mohan Embar
> Cc: address@hidden; Thong (Tum) Nguyen
> Subject: Re: [DotGNU]How do you simulate an inner class in C#?
> 
> If memory serves me right, Mohan Embar wrote:
> > >A java inner class is simply a class that has a reference to the
parent
> > >class.  You just need to pass a reference to the 'outer' class to
the
> > >'inner' class's constructor and store it somewhere.
> >
> > Thank you so much for your reply. Until I read your reply and then
reread
> > my C# book, I didn't realize that a C# nested class was allowed
access
> > to its outer class' private member variables too (provided it has
> > an instance). I'm still reconciling the similarities and difference
> > between both languages. I'm really glad I asked before coding
something
> > up. Thanks so much again.
> 
> Thanks for bringing this up right now ... The DotGNU Java to .NET
compiler
> was spitting out illegal code right now ...
> 
> So to clear up a point  ... how does Java figure out the OuterClass
> reference to pick up ?
> 
> ie
>       public Inner(Outer parent)
>       {
>           this.parent = outer;
>       }

Oops.  That should have been "this.parent = parent".

> 
> the "parent" argument in this case ?. Is it implictly provided by the
> compiler ?. If so , how ...
> 
> I would like to generate similar or identical semantics for our
compiler.
> 

Yeah it is implicitly added by the compiler.  AFAIK it works something
like this:

A "synthetic" argument which provides a reference to the outer class is
added as the first argument to every constructor in the inner class.

The inner class stores the reference to the outer class and uses it to
access the inner class.

Because the inner class just like any other class (except with a mangled
name) the JVM doesn't allow the inner class to access the private
members of outer class.  The Sun Java 1.4 compiler solves this by adding
package protected static "access" methods to the outer class for every
private member that the inner class accesses.

Here's an example:

public class Outer
{
   private int x =10;

   private void foo(double d)
   {
   }

   public void bar()
   {
   }

   public class Inner
   {
      public Inner(string s)
      {
         x = 20;
         foo(1.0);
         bar();
     }
   }
}

The compiler generates:

public class Outer
{
   private int x = 10;

   private void foo(double d)
   {
   }

   public void bar()
   {
   }
  
   static void access$001(Outer outer, int value)
   {
      outer.x = value;
   }

   static void access$002(Outer outer, double d)
   {
      outer.foo(d)
   } 
}

public class Outer$Inner
{
    private Outer $parent;

    public class Outer$Inner(Outer parent, string s)
    {
        this.$parent = parent;
       Outer.access$001(20);
       Outer.access$002(1.0);
        this.$parent.bar();
    }
}


You can test it out using "javap".

I don't think a .NET java compiler would need to use name mangling or
"access" methods.  .NET nested classes would be much easier :).

^Tum



reply via email to

[Prev in Thread] Current Thread [Next in Thread]