www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Implicitly static methods?

reply "Craig Black" <cblack ara.com> writes:
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'
	}
 
Mar 14 2005
parent reply Niall FitzGibbon <billdoor gmail.com> writes:
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 :)
Mar 14 2005
parent "Craig Black" <cblack ara.com> writes:
 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
Mar 14 2005