digitalmars.D - Implicitly static methods?
- Craig Black (21/21) Mar 14 2005 I found this code in the expressions section of the D documentation. Do...
- Niall FitzGibbon (9/38) Mar 14 2005 The method is not static, but when you do something like
- Craig Black (3/11) Mar 14 2005 Of couse! How dumb of me!
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
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
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