www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Calling Base Class Overriden Methods

reply "Jeroen Bollen" <jbinero gmail.com> writes:
How do I call a parent class's overidden method?

module test;

abstract class SuperClass {
     public pure void methodA() {

     }
}

class SubClass {
     public override pure void methodB() {
         // How do I call the parent methodA() from here?
     }
}
Nov 18 2013
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Monday, 18 November 2013 at 19:32:39 UTC, Jeroen Bollen wrote:
 How do I call a parent class's overidden method?
super.method so abstract class SuperClass { public pure void methodA() { } } class SubClass : SuperClass { public override pure void methodA() { // calls the parents super.methodA(); } } To do it from outside the class, you write the class name: void main() { auto obj = new SubClass(); obj.SuperClass.methodA(); // calls the specific super method }
Nov 18 2013
parent reply "Jeroen Bollen" <jbinero gmail.com> writes:
On Monday, 18 November 2013 at 19:34:56 UTC, Adam D. Ruppe wrote:
 On Monday, 18 November 2013 at 19:32:39 UTC, Jeroen Bollen 
 wrote:
 How do I call a parent class's overidden method?
super.method so abstract class SuperClass { public pure void methodA() { } } class SubClass : SuperClass { public override pure void methodA() { // calls the parents super.methodA(); } } To do it from outside the class, you write the class name: void main() { auto obj = new SubClass(); obj.SuperClass.methodA(); // calls the specific super method }
Thanks! :D Why aren't these things in the documentation? :/
Nov 18 2013
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Monday, 18 November 2013 at 19:36:07 UTC, Jeroen Bollen wrote:
 Why aren't these things in the documentation? :/
blargh, i thought it was, but can't find it either....
Nov 18 2013
prev sibling next sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Mon, Nov 18, 2013 at 08:36:06PM +0100, Jeroen Bollen wrote:
[...]
 Why aren't these things in the documentation? :/
Submit a bug to the bugtracker with "[dox]" in its subject line. This is the only way to guarantee the docs will improve. T -- Answer: Because it breaks the logical sequence of discussion. Question: Why is top posting bad?
Nov 18 2013
prev sibling parent "Kenji Hara" <k.hara.pg gmail.com> writes:
On Monday, 18 November 2013 at 19:36:07 UTC, Jeroen Bollen wrote:
 On Monday, 18 November 2013 at 19:34:56 UTC, Adam D. Ruppe 
 wrote:
 To do it from outside the class, you write the class name:

 void main() {
        auto obj = new SubClass();
        obj.SuperClass.methodA(); // calls the specific super 
 method
 }
Thanks! :D Why aren't these things in the documentation? :/
http://dlang.org/function#virtual-functions ========================= To avoid dynamic binding on member function call, insert base class name before the member function name. For example: ... void main() { auto d = new D(); assert(d.foo() == 3); // calls D.foo assert(d.B.foo() == 1); // calls B.foo assert(d.C.foo() == 2); // calls C.foo d.test(); } ========================= Kenji Hara
Nov 18 2013