View mode: basic / threaded / horizontal-split · Log in · Help
October 26, 2005
how does this nested class thing work?
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
October 26, 2005
Re: how does this nested class thing work?
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
October 26, 2005
Re: how does this nested class thing work?
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
> 
>
Top | Discussion index | About this forum | D home