www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - how does this nested class thing work?

reply dennis luehring <dl.soluz gmx.net> writes:
import std.stdio;

class Class
{
     this(){}
     class Method1
     {
	    this(){}
     	int parameter[2];
     	int result;
     	void call()
     	{
     		result = method1(parameter[0],parameter[1]);
     	}
     }

     int method1(int a, int b)
     {
     	return a + b;
     }
}

void main()
{
	Class c = new Class;
    	int x = c.method1(10,20);
    	writef("%d\n",x);

    	Class.Method1 m1 = new c.Method1();
    	m1.parameter[0] = 10;
    	m1.parameter[1] = 20;
    	m1.call();
    	writef("%d\n",m1.result);
}

i just try to write an simple oop-wrapper for an method
but i get this compile error:

"nested.d(29): no 'this' for nested class Method1"

from the d-docs
"A non-static nested class can only be instantiated when the necessary 
context pointer information is available"

i think there "is" a contex avaiable (through "c")

how can i solve the problem?

thx dennis
Oct 26 2005
parent reply Oskar Linde <Oskar_member pathlink.com> writes:
In article <djo1km$280i$1 digitaldaemon.com>, dennis luehring says...
[snip code]
"nested.d(29): no 'this' for nested class Method1"

from the d-docs
"A non-static nested class can only be instantiated when the necessary 
context pointer information is available"

i think there "is" a contex avaiable (through "c")
The compiler must know in which context the nested class should be created.
how can i solve the problem?
The context has to be available as the current this. You can only instanciate the nested class in this way from within a method of the outer class. /Oskar
Oct 26 2005
parent dennis luehring <dl.soluz gmx.net> writes:
Oskar Linde wrote:
 In article <djo1km$280i$1 digitaldaemon.com>, dennis luehring says...
 [snip code]
 
"nested.d(29): no 'this' for nested class Method1"
from the d-docs
"A non-static nested class can only be instantiated when the necessary 
context pointer information is available"

i think there "is" a contex avaiable (through "c")
The compiler must know in which context the nested class should be created.
in the contex of "c" i don't understand why d can't see the contex in "new c.Method1" c is the outer object(the contex) and Method1 is the inner class wouldnt it be nice to have such a feature?
 
 
how can i solve the problem?
The context has to be available as the current this. You can only instanciate the nested class in this way from within a method of the outer class.
that works for me - thx
 /Oskar
 
 
Oct 26 2005