View mode: basic / threaded / horizontal-split · Log in · Help
March 14, 2005
Implicitly static methods?
I found this code in the expressions section of the D documentation.  Do I 
understand this right?  It seems that the get() method is implicitly static. 
Do methods that don't access this pointers become implicitly static in D?

-Craig

class A
	{
	    char get() { return 'A'; }

	    char foo() { return typeof(this).get(); }
	    char bar() { return this.get(); }
	}

	class B : A
	{
	    char get() { return 'B'; }
	}

	void main()
	{
	    B b = new B();

	    b.foo();		// returns 'A'
	    b.bar();		// returns 'B'
	}
March 14, 2005
Re: Implicitly static methods?
Craig Black wrote:
> I found this code in the expressions section of the D documentation.  Do I 
> understand this right?  It seems that the get() method is implicitly static. 
> Do methods that don't access this pointers become implicitly static in D?
> 
> -Craig
> 
> class A
> 	{
> 	    char get() { return 'A'; }
> 
> 	    char foo() { return typeof(this).get(); }
> 	    char bar() { return this.get(); }
> 	}
> 
> 	class B : A
> 	{
> 	    char get() { return 'B'; }
> 	}
> 
> 	void main()
> 	{
> 	    B b = new B();
> 
> 	    b.foo();		// returns 'A'
> 	    b.bar();		// returns 'B'
> 	}
>  
> 
> 

The method is not static, but when you do something like 
typeof(this).get() or A.get() within a method of the class (or one 
derived from it), the current value of "this" is passed.  The reason it 
calls A.get() rather than B.get() is because the any call made with 
typeof(this).get() is non-virtual -- since you define foo() in class A 
and do not override it in class B, the non-virtual call is made within 
class A.

At least, that's my understanding of it :)
March 14, 2005
Re: Implicitly static methods?
> The method is not static, but when you do something like 
> typeof(this).get() or A.get() within a method of the class (or one derived 
> from it), the current value of "this" is passed.  The reason it calls 
> A.get() rather than B.get() is because the any call made with 
> typeof(this).get() is non-virtual -- since you define foo() in class A and 
> do not override it in class B, the non-virtual call is made within class 
> A.
>
> At least, that's my understanding of it :)

Of couse!  How dumb of me!
Thanks,

Craig
Top | Discussion index | About this forum | D home